开发者

regular expressions - my expression

I've got (To) [a-z]+ as regular expression and I've got sentence: To kot dziki pies.

And If I compile it I will retrieve To kot.

So what can I do to retrieve only word开发者_如何学Go after (only kot) "To" instead of "To kot"?


^To (\w+)$ should do the trick. \w is shorthand for any word character, eg. a-z in English; other characters in other languages. If you put parens around To as in your example, it will create a matched group, which means the match for [a-z]+ will be in the second group, and To will be in the first group.

I can really recommend using an interactive tool for testing and developing regular expression, such as Expresso.


use groups inside the regular expression - "To ([a-z]+)" and then retrieve group 1's value, it will contain "kot" supplied your example string.


Or you could use lookbehinds:

(?<=To )[a-z]+

Then the To does not become part of the captured expression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜