开发者

Regexp match any character except a particular string

开发者 https://www.devze.com 2023-04-07 18:56 出处:网络
I am using the regexp, /(\\<\\s*?string(-array)?\\s*?.*?\\s*?\\>\\s*?)(.*)(\\s*?\\<\\/string(-array)?\\>)/

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>
</strin开发者_Python百科g-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'. I tried a negative look-behind for 'str' immediately preceding the content ('item-matching') group, and it is giving me the same results.

Any help would be great!


You need to use SimpleXML to parse XML. The XML may change or not match your regex in edge cases - so it's best to just use an XML parser.

<?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.

In any case this should work :

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


You really should not parse xml using regexps.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消