0

I have the following expression to validate a specific tag:

<TAG(:([^:>\\s]*))?>

This pattern should be valid for: <TAG:abc>, <TAG:dfg>, etc

How to make this expression be invalid in this case: <TAG:hij>k> (k> = invalid closing tag).

3
  • Which language are you running. If it's java then you need to escape the backslash one more time. Commented Oct 7, 2014 at 6:21
  • <TAG(:([^:>\\s]*))?>(?=\s|$) is correct! Thank you Commented Oct 7, 2014 at 6:24
  • \\s inside a character class would mean a literal \ and an literal s character. It wouldn't match a space character. Commented Oct 7, 2014 at 6:25

1 Answer 1

1

Just use a lookahead after to your regex to ensure that the tag must be followed by a space or end of the line anchor.

<TAG(:([^:>\\s]*))?>(?=\s|$) 

OR

<TAG(:([^:>\s]*))?>(?=\s|$) 

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

worked perfectly! RegEx is something that I need to study a little more... Thank you to solve a little bug in my library =D

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.