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 using PHP preg_match() function
The good news is that you can use lookbehind anywhere in the regex, not only at the start.
Negative Lookbehind Syntax:Where match is the item to match and element is the character, characters or group in regex which must not precede the match, to declare it a successful match. So if you want to avoid matching a token if a certain token precedes it you may use negative lookbehind. For example / (? <!
sed does not support lookaround assertions. For what it's worth, grep -P is also a nonstandard extension, though typically available on Linux (but not other platforms).
Apart from looking ahead, you can also look back (or "behind," as it's called in the world of GREP). The general format for positive lookbehind is (? <=) .
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";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With