0

Hi i want a regular expression match all words with first letter uppercase. But there is this greedy problem.

If i use \<[[:upper:]].*\> It will start matching at word starting with uppercase letter, but will end at the end of a last word on a line (with Grep)

So i tried \<[[:upper:]][^\>]*\> "Start of a word, any Uppercase letter, any count of any character which is not end of a word and then end of a word"

So why is the result still same?

  • perhaps [[:upper:]][[:lower:]]+ gets you closer? Unless A counts, in which case turn the + to a * – Jeff Schaller Dec 6 '18 at 14:59
  • The results are the same for grep because grep doesn't need to see the subtle difference between the two expressions. In the first expression .* matches anything until the last >. In the second expression [^\>]* matches anything until the first occurence of >. In both cases grep returns a match and doesn't care whether it's matching the first or last occurence of >. – Colin Pearse Apr 12 at 15:29
0

Try this,

grep -E -o '[[:upper:]][[:lower:]]+' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.