Match a string that does not contain a specific sequence of characters

I am trying to use regular expressions to match a string that does not contain a sequence of characters less than a character (<) followed by a space. Here are some examples

Valid - "A new description."


Valid - "A < new description."


Invalid -"A <new description."

I can't seem to find the correct expression to get the match. I am using the Microsoft regex validator, so I need it to be a match, not use code to deny the match.

Any help would be appreciated.

Thanks,
Dale

+2


source to share


5 answers


@"^(?:[^<]+|<(?!\s))*$"

      

Doing a negative lookahead for space allows it to match if the last character on the line is "<". Here's another way:



^(?!.*<\S).+$

      

Looker scans the entire line for "<" followed by a character without spaces. If not found, ". +" Goes ahead and matches the string.

+6


source


In other words, you are allowing two things in your line:

  • Any character EXCEPT a <

  • A <

    followed by a space.


We can write it pretty straightforward as:

/([^<]|(< ))+/

      

+1


source


var reg = new Regex(@"<(?!\s)");
string text = "it <matches";
string text2 = "it< doesn't match";

reg.Match(text);// <-- Match.Sucess == true
reg.Match(text2);// <-- Match.Sucess == false

      

0


source


Use negative forward outlook: "<(?!)"

0


source


I think this might be what you are looking for.

Valid - "A new description."
Valid - "A < new description."
Invalid - "A <new description."



 Try this:   <\S

      

This looks for something with less than a character and no space after it.

In this case, it will match "<n"

Not sure how much you want it to match.

0


source







All Articles