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?

2
  • perhaps [[:upper:]][[:lower:]]+ gets you closer? Unless A counts, in which case turn the + to a *
    – Jeff Schaller
    Dec 6, 2018 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 >. Apr 12, 2019 at 15:29

1 Answer 1

1

Try this,

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .