正则表达式。 如何在分号后得到单词
问题描述:
I'm trying to get all the words(surnames) only after every semicolon appearance.
I've tried this regex, but it's not working correctly:
(?:.*?;)([a-zA-ZñÑ]+)
Here's the example line I want to filter with regex:
Walter Shelter, Mary; Johnson Smith, Robert; Dickinson Queen, Patty
Here's the result I want to get:
Walter Johnson Dickinson
Beforehand, thanks a lot for your help!
我试图在每个分号出现后才得到所有单词(姓氏)。 p>
我试过这个正则表达式,但是它运行不正常: p>
(?:。*?;)([a-zA-ZñÑ] +)
code> pre>
以下是我想用正则表达式过滤的示例行: strong> p>
玛丽的沃尔特·谢尔特; 约翰逊史密斯,罗伯特 Dickinson Queen,Patty p>
blockquote>
以下是我想得到的结果: strong> p>
Walter Johnson Dickinson p>
blockquote>
事先,非常感谢你的帮助! p>
div>
答
You can use this regex:
(?:(?<=^)|(?<=; ))\w+
Or this shorter one:
(?<=; |^)\w+
Code:
$s = 'Walter Shelter, Mary; Johnson Smith, Robert; Dickinson Queen Patty';
preg_match_all('/(?<=; |^)\w+/', $s, $m);
print_r($m);
Output:
Array
(
[0] => Walter
[1] => Johnson
[2] => Dickinson
)