Reputation: 23941
I want to write a regex that will pull out the phrases of capitalized [a-z]
words. So if it sees this phrase it should pull out "America Fremerica" and "King" from
there is a land called America Fremerica where regex is King
I have a regex ([A-Z][a-z]+ ?){1,}
that pulls out Fremerica and King.
I want it to pick out America Fremerica
. Why doesn't it pick out America
? Is that why it does not pick out the phrase?
Upvotes: 0
Views: 263
Reputation: 425448
Your regex captures the trailing space. This regex captures a capitalized word followed by 0-n more such words (either as the whole match or group 1 - they are the same), which captures just "America Fremerica"
(not "America Fremerica "
)
([A-Z][a-z]+(?: [A-Z][a-z]+)*)
See a live demo
Upvotes: 1
Reputation: 362197
Your regex works, but it's not capturing all of the words. The regex (a)+
will match the string aaa
but it will only capture the last a
. To capture all three a
s you'd need to write (a+)
with the wildcard inside the parentheses.
So put another set of parentheses around the whole thing. You want to capture the repetitions. You can also change {1,}
to +
, which is equivalent.
((?:[A-Z][a-z]+ ?)+)
?:
stops the inner set of parentheses from being a capture group. It's not necessary, but it's nice to have.
Upvotes: 3
Reputation: 88
It appears to work as expected in Javascript. See this fiddle: http://jsfiddle.net/9X83F/2/
HTML
<p id="result"></p>
Javascript
var phrase = "there is a land called America Fremerica where regex is King";
var matches = phrase.match(/([A-Z][a-z]+ ?){1,}/g);
document.getElementById('result').innerHTML = matches;
Upvotes: 0