问题
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