gpt4 book ai didi

regex - 获取前后的句点和单词(重叠匹配)

转载 作者:行者123 更新时间:2023-12-01 12:41:28 26 4
gpt4 key购买 nike

我想获取某些文本中的所有句号以及周围的单词。下面的文字可以作为一个例子:

This study was aimed at designing production of isoeugenol and vanillin from eugenol of clove leaf oil and analyzing the potential product development financially. The specific objectives of this research work are: 1. Identify the isoeugenol and vanillin. 2. Model simulation of process design of isoeugenol and vanillin. 3. Study on financial feasibility and added value. This research is expected to provide maximum economic potential of eugenol to enhance the added value of clove leaf oil. The results showed that the FTIR and NMR products confirmed that isoeugenol and vanillin present in the synthesized product were identical to the reference standards.

当我使用模式时

\w+\.\s\w+

在上面的字符串中,它匹配(来自 and vanillin.2. Model simulation 部分)vanillin。 2 但它会跳过 2。型号

我希望它与 vanillin 都匹配。 22。型号.

你能给我一些改进,让我得到所有的月经吗?

最佳答案

使用 positive lookahead assertion连同 capturing group :

(?=(\b\w+\.(?:\s+\w+|$)))

按如下方式使用:

preg_match_all('/(?=(\b\w+\.(?:\s+\w+|$)))/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[1];

解释:

(?=       # Assert that the following can be matched at the current position:
( # Capture into group number 1:
\b # - Beginning of a word
\w+ # - an alphanumeric word
\. # - a dot
(?: # - Then either...
\s+\w+ # - whitespace and another word
| # - or...
$ # - the end of the string.
) # End of alternation
) # End of capturing group 1
) # End of lookahead

See it in action on regex101.com .

关于regex - 获取前后的句点和单词(重叠匹配),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17103416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com