0

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
Biffen
  • 5,354
  • 5
  • 27
  • 32
Ash McConnell
  • 1,322
  • 1
  • 16
  • 28
  • 1
    you mean this http://regex101.com/r/dK5dX1/1 ? This `|\s.*>))\/?.*?>` would do the opposite of regex you mentioned. – Avinash Raj Sep 10 '14 at 10:51
  • It doesn't seem to work in Java for some reason (sorry should have said) - checking here: - http://www.regexplanet.com/advanced/java/index.html – Ash McConnell Sep 10 '14 at 11:07
  • @AshMcConnell Better to always specify the regex flavor/language, if specific. – Jonny 5 Sep 10 '14 at 11:13

3 Answers3

1

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

Community
  • 1
  • 1
Jonny 5
  • 11,051
  • 2
  • 20
  • 42
1
(?=^[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

vks
  • 63,206
  • 9
  • 78
  • 110
1

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
Avinash Raj
  • 160,498
  • 22
  • 182
  • 229