简体   繁体   English

匹配包装图案

[英]Matching a wrapped pattern

What regex pattern can be used for finding word with a following dot that are in a parentheses pair. 哪种正则表达式模式可用于查找括号对中带有后一个点的单词。

as. 如。 df.kj hlasfa ( asd. las kdfh. ) dfs adk jfh. df.kj hlasfa(ASD。拉斯kdfh。)DFS ADK JFH。 a (sd.kf) jhd (dsk fh. ) dskfdf asdf dfs lkjh asdfdsfjkhlkjh ( asdf. )as fadf asf a(sd.kf)jhd(dsk fh。 )dskfdf asdf dfs lkjh asdfdsfjkhlkjh( asdf。 )as fadf asf

Desired results are bolded. 期望的结果以粗体显示。

You could use a lookahead , that asserts that there is no opening parenthesis before the next closing one: 您可以使用lookahead ,它断言在下一个结束括号之前没有开头括号:

preg_match_all('/[a-zA-Z]+\.(?![a-zA-Z])(?=[^()]*\))/', $input, $matches);

You will find the four matches in $matches . 您将在$matches找到四个$matches

The first negative lookahead eliminates the case mentioned by HamZa in the comment. 第一个否定的前瞻消除了HamZa在评论中提到的情况。 The second (positive) lookahead tries to find a closing parenthesis before the next opening one. 第二个(正)前行尝试在下一个打开的括号之前找到一个闭合的括号。

There is a caveat here. 这里有一个警告。 This will start to crumble once you have nested parentheses. 一旦嵌套了括号,这将开始崩溃。 some(text. (here)) would not match text. some(text. (here))将不匹配text. . Nested patterns (which can occur from using parentheses) are generally beyond the scope of regular expressions, although some engines can partly handle them. 嵌套模式(可以通过使用括号产生)通常超出了正则表达式的范围,尽管某些引擎可以部分处理它们。 If this is the case for you, you are better of going through the string manually and counting parentheses. 如果是这种情况,则最好手动遍历字符串并计算括号。

许多选择之一是:

\b[a-zA-Z]+[.](?=[ )])(?=[^(]*[)])        

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM