regular expressions: how to find the bit between the “<>”

时光毁灭记忆、已成空白 提交于 2020-12-25 10:46:56

问题


In the following string,

Jason <jason@bigcreative.com>

How can i extract the part inside the angle brackets. I tried <\w> and it didn't work.

Ideas? I'm using preg_match() in php if that makes a difference.


回答1:


Use <(.*?)> as regex, then.




回答2:


user502515 has already given the regex you want.

I'd like to add why your regex <\w> did not work:

\w is the short for the character class [a-zA-Z0-9_] and matches any one character from that class. To match more characters you need to use quantifiers:

  • + for one or more and
  • * for zero or more

Since you want to extract the string matching the pattern you need to enclose the pattern in parenthesis (..) so that it gets captured.

Now your original task was to extract the string between <..>, the regex <(\w+)> will not do the job as the char class \w does not include @.

To match anything you use the regex .* which matches any arbitrary string (without newline).

So the regex <(.*)> matches and captures any string between the angular brackets.

The match is greedy, so if the input string is foo<foo@foo.com>, bar<bar.com> you'll be extracting foo@foo.com>, bar<bar.com. To fix this you make the match non-greedy by adding a ? at the end of .* giving us the correct regex <(.*?)>



来源:https://stackoverflow.com/questions/4373508/regular-expressions-how-to-find-the-bit-between-the

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!