gpt4 book ai didi

php - 捕获包含 "."或 ":"但不以句点结尾的行

转载 作者:搜寻专家 更新时间:2023-10-31 20:53:36 25 4
gpt4 key购买 nike

我正在尝试创建一个包含字符集的正则表达式,该字符集可以包含句点或冒号,但不能以句点结尾。所以我想写一行 "Lorem./: Ipsom dolor sit" 而不是 "Lorem ipsum dolor sit."

这是我当前的正则表达式的样子,但它不起作用,因为如果字符串以句点或冒号结尾,它将匹配:

/(\n{2,})([\wåäöÅÄÖ,()%+\-:.]{2,75}[^.:])(\n{1,})/

我正在寻找一个巨大的、格式错误的纯文本文件中的标题。此文件中的一般模式是,标题前面总是有两个或更多换行符,后面总是有一个或更多换行符。此外,标题有时以 : 结尾,但从不以 . 结尾,但它们有时包含 .:。此外,它们的长度始终为 2-75 个字符,并且从不以其他标题开头。

如有任何帮助,我们将不胜感激。

编辑:我意识到我的解释很糟糕并且部分错误,因此更新了这篇文章。

最佳答案

一般情况下,如果要匹配不以点结尾的字符串,只需要加上(?<!\.)$即可。到正则表达式的末尾。

这是一个 negative lookbehind assertion .

在你的特殊情况下,比赛应该在此之后继续,所以我们需要一种不同的方法:

/\n{2,}([ \wåäöÅÄÖ,()%+\-:.]{2,75}(?<!\.))\n+/

将匹配任何一行

  • 后跟两个或多个换行符 ( \n{2,} ),
  • 仅包含 2 到 75 个允许的字符 ([ \wåäöÅÄÖ,()%+\-:.]),
  • 不以 . 结尾( (?<!\.) - )
  • 并且后跟至少一个换行符 ( \n+ )。

编辑:

一个新的、扩展的正则表达式,试图合并下面评论中讨论的一些逻辑;格式化为详细的正则表达式:

preg_match_all(
'/(?<=\n\n) # Assert that there are two newlines before the current position
^ # Assert that we\'re at the start of a line
(?![\d -]+$) # Assert that the line consists not solely of digits, spaces and -s
# Assert that the line doesn\'t consist of two Uppercase Words
(?!\s*\p{Lu}\p{L}*\s+\p{Lu}\p{L}*\s*$)
# Match 2-75 of the allowed characters
[ \wåäöÅÄÖ,()%+\-:.]{2,75}
(?<!\.) # Assert that the last one isn\'t a dot
$ # Assert position at the end of a line
(?=\n) # Assert that one newline follows.
/mxu',
$subject, $result, PREG_PATTERN_ORDER);

关于php - 捕获包含 "."或 ":"但不以句点结尾的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4991750/

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