Regular expression lookbehind problem
I use
(?<!value=\")##(.*)##
to match string like ##MyString## that's not in the form of:
<input type="text" value="##MyString##">
This works for the above form, but not for this: (It still matches, should not match)
<input type="text" value="Here is my ##MyString## coming..">
I tried:
(?<!value=\").*##(.*)##
with no luck. Any suggestions will be deeply appreciated.
Edit: I am us开发者_开发问答ing PHP preg_match() function
This is not perfect (that's what HTML parsers are for), but it will work for the vast majority of HTML files:
(^|>)[^<>]*##[^#]*##[^<>]*(<|$)
The idea is simple. You're looking for a string that is outside of tags. To be outside of tags, the closest preceding angled bracket to it must be closing (or there's no bracket at all), and the closest following one must be opening (or none). This assumes that angled brackets are not used in attribute values.
If you actually care that the attribute name be "value", then you can match for:
value\s*=\s*"([^\"]|\\\")*##[^#]*##([^\"]|\\\")*\"
... and then simply negate the match (!preg_match(...)
).
@OP, you can do it simply without regex.
$text = '<input type="text" value=" ##MyString##">';
$text = str_replace(" ","",$text);
if (strpos($text,'value="##' ) !==FALSE ){
$s = explode('value="##',$text);
$t = explode("##",$s[1]);
print "$t[0]\n";
}
here is a starting point at least, it works for the given examples.
(?<!<[^>]*value="[^>"]*)##(.*)##
精彩评论