Reputation: 1337
We need to match text from a user input, but specifically reject any tags that aren't <br>
.
From other stackoverflow posts I can find the opposite match to what I need (i.e. it matches the offending tags rather than the text and the other tag). Due to constraints we can't use negative logic to this for validation. The regex is:
<(?!\/?br(?=>|\s.*>))\/?.*?>
Is it possible to match the whole text if it only contains "normal" text and BR tags?
For example these should match:
bob
bob<br>bob
bob<br />bob
bob</br>
These should fail to match
bob<p>bob
bob<div>bob
bob</div>bob
Upvotes: 0
Views: 93
Reputation: 174696
The below regex would work,
String s = "bob\n" +
"bob<br>bob\n" +
"bob<br />bob\n" +
"bob</br>\n" +
"bob<p>bob\n" +
"bob<div>bob\n" +
"bob</div>bob";
Pattern regex = Pattern.compile("^\\w+(?:<(?=\\/?br(?=>|\\s.*>))\\/?.*?>(?:\\w+)?)?$", Pattern.MULTILINE);
Matcher matcher = regex.matcher(s);
while(matcher.find()){
System.out.println(matcher.group(0));
}
Output:
bob
bob<br>bob
bob<br />bob
bob</br
Upvotes: 1
Reputation: 12389
Could use two negative lookaheads:
(?si)^(?!.*<(?!\/?br\b)\w).*
as a Java string:
"(?si)^(?!.*<(?!\\/?br\\b)\\w).*"
Used s
(dot match newline too), i
(caseless) modifier.
test at regexplanet (click on Java); test at regex101; see SO Regex FAQ
Upvotes: 1
Reputation: 67968
(?=^[a-zA-Z0-9]+$|[^<>]*<\s*(\/)?\s*br\s*(\/)?\s*>[^<>]*)^.*$
You can try this.This use postive lookahead.See demo.
http://regex101.com/r/kO7lO2/4
Upvotes: 1