I need a regular expression that does not match when a character exists in string after a specific character.
For example my first character is 'X' and the character that is no allowed after is 'U'.
Following is valid:
eUpXssseeeree // U is before X
rtsssXeeeree // U is not there at all
Following is NOT valid
ereXUppsrrrsssss // U is there right after X
ereeXrerrrtogUoosss // U is still there even after a few characters
I'm using C# syntax. Can you guide me on how to write such a regular expression?
Sounds like it could be as straightforward as "any characters, followed by X, followed by any characters that aren't U":
/^.*X[^U]*$/
(see reFiddle example)
Of course, you want to make sure to match the entire string by using start ^ and end $ characters.
string testString = "eUpXssseeeree";
if ( !Regex.IsMatch( testString , @"[X].*[U]") )
{
//Valid case
//eUpXssseeeree // U is before X
//rtsssXeeeree // U is not there at all
}
else
{
//Invalid Case
}
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