Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

Matching a whole string against a regular expression

$
0
0
Hi Guys

I'm not particularly string on regular expressions and this feels like it should be easy but I'm coming unstuck. I'm parsing some input strings and looking to make sure they match a known format. Regular expressions fell like the way I should go but I'm open to being corrected.

The basic format I'm looking for is is any string with an underscore and a number as a suffix. So for example:-
"Test_1" is a match
"Test" is not a match
"Test_With_Underscores_1" is a match
"Test_With_Underscores" is not a match
"Test_With_1_Underscores" is not a match

So far I've got the following pattern:-
.+_[0-9]+
and I'm using that with the IsMatch method to check the string. It's great for the first four example above but it fails on the fifth. It returns true where I would expect false. I think that's because it's looking for the pattern in any part of the string but I want it to match the whole string. How can I do that? Do I need to use a different method? Call IsMatch with different options? Use a different pattern? I'm struggling to figure it out.

Just FYI, I'm using the . for any character becasue I want to allo "special" characters, %$*, that sort of thing. I gather it'll let through unprintable characters as well but that won't be relevant in my case. I also know it'll exclude line breaks etc, but again, that's not a consideration in my scenario.

So, any help would be apreciated. I'm sure I'm missing something quite simple here.

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>