简体   繁体   English

正则表达式匹配除特定字符串外的任何字符

[英]Regexp match any character except a particular string

I am using the regexp, 我正在使用正则表达式,

/(\<\s*?string(-array)?\s*?.*?\s*?\>\s*?)(.*)(\s*?\<\/string(-array)?\>)/ 

... to match all content between or tags of the form: ...以匹配形式之间的所有内容或标记:

<string-array name="saveArray">
  <item>Téléphone</item>
  <item>Carte mémoires</item>
</string-array>

Problem is, I'm only able to match the contents of 'string' tags or arrays containing one item. 问题是,我只能匹配“字符串”标签或包含一项的数组的内容。 When I replace the dot from the captured group in the middle with [^s], I get the content I want, but this solution would fail to match any content containing 's'. 当我用[^ s]替换中间捕获组中的点时,我得到了想要的内容,但是此解决方案将无法匹配任何包含's'的内容。 I tried a negative look-behind for 'str' immediately preceding the content ('item-matching') group, and it is giving me the same results. 我尝试在内容(“项目匹配”)组之前紧随其后的否定“ str”,它给了我相同的结果。

Any help would be great! 任何帮助将是巨大的!

You need to use SimpleXML to parse XML. 您需要使用SimpleXML来解析XML。 The XML may change or not match your regex in edge cases - so it's best to just use an XML parser. 在某些情况下,XML可能会更改或不匹配您的正则表达式-因此最好只使用XML解析器。

<?php
$xml '<string-array name="saveArray">'
. '<item>Téléphone</item>'
. '<item>Carte mémoires</item>'
. '</string-array>';

$items = new SimpleXMLElement($xml);

As others have said do not use regex to parse xml/html. 正如其他人所说, 不要使用正则表达式来解析xml / html。

In any case this should work : 无论如何,这应该可行:

if ($subject =~ m!<(string-array)[^>]*>(.*?)</\1>!si) {
    print $2, "\n";
}

You really should not parse xml using regexps. 实际上不应该使用正则表达式解析xml。

That said, I think the thing that's messing you up might be that " . " (in many regexp engines, with default flags) matches any character except a newline.. So your .* will not match more than one line. 就是说,我认为让您感到困惑的可能是“ . ”(在许多正则表达式引擎中,带有默认标志)与换行符之外的任何字符匹配。因此,您的.*最多只能匹配一行。 Try replacing " .* " with " [\\w\\W]* ", or adding a regexp flag that says that " . " should match all characters. 尝试将“ .* ”替换为“ [\\w\\W]* ”,或添加一个表示“ . ”应匹配所有字符的正则表达式标志。

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

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