objective c - Match first word of a phrase in a String -
i using gtmregex class google toolbox mac (in cocoa / objective-c) app:
http://code.google.com/p/google-toolbox-for-mac/
i need match , replace of 3 word phrase in string. know 2nd , 3rd words of phrase, first word unknown.
so, if had:
lorem biff bam boo ipsem
and
lorem beep bam boo ipsem
i watch match both (beep bam boo) , (biff bam boo). want wrap them in bold html tags.
here have:
gtmregex *requiredheroregex = [gtmregex regexwithpattern:@"(\\([a-z][a-z0-9]*)\\b hero required)" options:kgtmregexoptionsupressnewlinesupport|kgtmregexoptionignorecase]; out = [requiredheroregex stringbyreplacingmatchesinstring:out withreplacement:@"<b>\\1</b>"];
however, not working. basically, cant figure out how match first word when dont know it.
anyone know regex this?
update:
gtregex uses posix 1003.2 regular expresions, solution is:
gtmregex *requiredheroregex = [gtmregex regexwithpattern:@"([[:<:]][a-z][a-z0-9]*[[:>:]])( hero required)" options:kgtmregexoptionsupressnewlinesupport|kgtmregexoptionignorecase]; out = [requiredheroregex stringbyreplacingmatchesinstring:out withreplacement:@"<b>\\1\\2</b>"];
note crazy syntax word boundaries.
update 2 : here javascript version:
/(([a-za-z]*?|[a-za-z]*? [a-za-z]*?)( hero required))/gm
you should use " .*? hero required"
, however, not catch phrase if start of sentence. both cases use "( .*? hero required|^.*? hero required)"
.
Comments
Post a Comment