开发者

regular expressions - my expression

开发者 https://www.devze.com 2022-12-27 18:51 出处:网络
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.

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

精彩评论

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

关注公众号