我理解正则表达式如何搭配文字与没有条件限制它
例如一个问题,我有以下文字:
To ask Jack the Daniels, John the Ripper, whether, they will go out.
而且我想这部分匹配 - Jack the Daniels, John the Ripper
,它结束逗号和后小写字母([AZ])*
当我与此正则表达式匹配它:
'To ask (?P<addressee>[A-Za-z,\s]*), [a-z]'
我得到以下结果:
Jack the Daniels, John the Ripper, whether
因此问题是我可以用没有在括号内调理?像这样的事情,但它不是为我工作:
'To ask (?P<addressee>[A-Za-z,\s]*(^, [a-z]*)), [a-z]'
----编辑
看来,如果我将像这样的东西与之匹配: “(?:\ W [AZ] *) ” 和使用preg_match_all我会得到我想要的东西,但文字可以像下面太:
To ask Jack the Daniels, John the Ripper, whether, they will go out with Joe the Man, or me.
我将匹配Joe the Man
太多,我不想。在条件检查之前是否需要使用([,a-z])?
以下不工作,如何纠正?
'(?<!(, [a-z]))(\W[A-Z].*?),'
我在这里测试http://www.spaweditor.com/scripts/regex/index.php – llamerr 2011-02-28 11:16:58
你想匹配整个字符串'杰克丹尼尔斯,约翰开膛手'? – hsz 2011-02-28 11:36:47
是的,从“要问”直到第一个小写字母后面的逗号 – llamerr 2011-02-28 11:40:02