1

need a pattern to match a string which should NOT be --

777777777
888888888
999999999

or start with 00 or 02 or 04.

when i tried to go create a pattern to match the above requirements, i got it done by -

Dim _pattern6 As String = "^(7+|8+|9+|(00|07|08|09|17|18|19|28|29|43|48|69|70|78|79|80|96|97).*)$"

could not get the NOT MATCH part done.

0

3 Answers 3

3

What you want is negative lookahead.

@"^(?!([789])\1{8}$|0[024]).*$"

The negative lookahead (?!...) means "whatever follows this position cannot match any of these patterns." So (?!7{9}).* means "any string of characters (.*) that doesn't start with nine 7s in a row." The ([789])\1{8}$ is shorthand for 9 repeated digits. It means "Either 7, 8, or 9 followed by itself another 8 times."

Tested on RegexPlanet: http://fiddle.re/tz8p

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

2 Comments

I've never tried putting two $ 's at the end of a regex, does it work? (though I suppose you aren't matching it just looking at it the first time). So, +1 to you sir.
@jb - Correct: The first $ is just saying that whatever follows is going to be something other than 9 of the same number [7-9] and an end-of-line. $ is valid anywhere in a regex as long as the result still makes sense, so (^foo$|^bar$|^baz$) would work fine, whereas foo$bar$ wouldn't. Thanks for the +1. :)
1

You could try to match to unwanted parts. If that returns true, you know the attempt to "not match" would have been false, and vice versa.

Comments

-2

Here you go

[^(7{9}|8{9}|9{9}|00|02|04)]+

3 Comments

This isn't how negative character classes work. If you don't get a runtime error, I'm pretty sure this is going to look for literal versions of the special characters in the group.
this simplifies down to [^024789]+ ie "anything other then these numbers"
@Justin's right; [^(7{9}|8{9}|9{9}|00|02|04)] is equivalent to [^024789(){}|], which matches any one character as long it's not one of those in the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.