Php 如何找到每件事,但不是一个字
在文本中,我想找到类似于每件事的结构,直到某个文本,但某些单词之间不匹配 文本示例:Php 如何找到每件事,但不是一个字,php,regex,preg-match,Php,Regex,Preg Match,在文本中,我想找到类似于每件事的结构,直到某个文本,但某些单词之间不匹配 文本示例: Templates : You can add custom templates for your theme. Updated on 2010 look[124] end Media RSS feed : Add the Cooliris Effect to your gallery Updated on 2011 look[124] Role settings : Each gallery has a au
Templates : You can add custom templates for your theme. Updated on 2010 look[124] end
Media RSS feed : Add the Cooliris Effect to your gallery Updated on 2011 look[124]
Role settings : Each gallery has a author Updated at 2010 ... look[124] end
AJAX based thumbnail generator : No more server Updated on 2010 look[124] end limitation during the batch process Copy/Move : Copy or move images between Updated on 2010 this look[124] galleries Sortable Albums : Create your own sets of images Updated on 2010 this look[124] end
Upload or pictures via a zip-file (Not in Safe-mode)
Watermark function : You can add a watermark image or text
...
我需要找到“Updated.[124]end”每个匹配都必须以“[number]”和“end”结束。但有些文本看起来非常相似,但不是以单词“end”结尾。此文本必须而不是马赫数。如何让它工作
我试着写作
/Updated(.*?)\[.*?\]\send/msi
或
但这需要以下字符串:
Updated on 2011 look[124] Role settings : Each gallery has a author Updated at 2010 ... look[124] end
Updated on 2010 this look[124] galleries Sortable Albums : Create your own sets of images Updated on 2010 this look[124] end
如何编写regex witch跳过坏匹配
谢谢您的意见。假设所有无效匹配都有一个
[124]
,但没有一个结束
,您可以通过在更新的
和结束序列之间不允许一个[
来过滤掉它们,如下所示:
Updated([^[]*?)\[\d*\]\send
使用lazy regexp
Updated.*?\[.*?\]( end)?
一种可能性:
Updated([^[]*)\[124\]\s+end
说明:
Updated # Word 'updated'
[^[]* # All chars until '['
\[124\] # String '[124]'
\s+ # One or more spaces.
end # String 'end'
要匹配不包含
Updated
的字符串,可以使用以下结构:
(?:[^U]+|U(?!pdated))*
及
使用第一个备选方案会得到如下表达式:
Updated((?:[^U]+|U(?!pdated))*)\[\d+\]\send
第一个备选方案解释如下:
(?: # non-capturing group
[^U]+ # any characters that aren't "U"
|U(?!pdated) # or a "U" which is not followed bu "pdated" (ie. not "Updated")
)* # repeated as much as possible
第二种选择:
(?: # non-capturing group
(?!Updated). # Use a lookahead check at every character to make sure it's not "Updated"
)* # repeated as much as possible
也许您可以尝试另一种方法:
/Updated[\w.\s]*\[\d+\]\send/
说明:
Updated
这将匹配单词Updated
[\w\d.\s]*
然后是所有字母、数字、空格和点(你可以随意添加任何字符)
然后是括号中的数字
\send
而不是一个空格,最后是单词end,我想这就是你用第二个正则表达式的目的:
Updated\s++(?>(?!Updated\b|end\b)\S+\s+)*+end\b
换句话说,匹配Updated
,并查找相应的end
。如果您首先找到另一个Updated
,您知道您从错误的位置开始,因此放弃该匹配。我也排除了end
,因为这使我能够以占有方式匹配单词(即使用*+
);正则表达式从不需要回溯才能找到或(更重要的)消除匹配
如果您真的必须指定look[nnn]
部分,那么应该这样做:
Updated\s++(?>(?!Updated\b|end\b|look\[\d+\])\S+\s+)*+look\[\d+\]\s+end\b
如果需要,为不区分大小写的匹配添加i
标志,但您不需要m
或s
标志。如果这看起来过于复杂,那是因为我不像您那样了解您的数据。很可能这就是您真正需要的:
Updated(?:(?!Updated).)*\send
而且,请记住regexen是贪婪的,它们会尝试匹配一行中最长的内容,而不是
(\w*\d*\.*\s*)*
您的意思是[\w.\s]*
+1,我肯定会选择第二种方法。第一种方法可能会更快,但不会太快。无论如何,这还不足以证明可读性的重要性。请参阅我对性能崇拜者方法的回答。;)
Updated\s++(?>(?!Updated\b|end\b)\S+\s+)*+end\b
Updated\s++(?>(?!Updated\b|end\b|look\[\d+\])\S+\s+)*+look\[\d+\]\s+end\b
Updated(?:(?!Updated).)*\send