Question

I am using C# pattern matching with Regular Expressions using Visual Studio 2010.

So my issue is I want to match strings like:

dog1dog235cat7 Winners

lizard2433cat23dog44 Losers

dog23 Winners

where I have letters followed by some digits then followed by 0 or more letter/digit combos. There will always be a space followed by some phrase.

I am trying to figure out how to discriminate against things like "dog7 bones and treats".

The pattern I currently came up with is:

[a-zA-Z]+[0-9]+([a-zA-Z]+[0-9]+)*\s\w

Issue is I have not found any good information on testing for a block of pattern that occurs 0 or more times. So I don't know if there is a good grouping character that indicates this block can occur 0 or more times. I am attempting this with the parenthesis in the ([a-zA-Z]+[0-9]+)* though I believe that is normally used with the Group keyword to pull out instances of part of a pattern for later use.

So does anyone know how I can get the piece of the pattern that is [a-zA-Z]+[0-9]+ be checked for occuring 0 or more times?

(I've looked around, but I haven't seen a C# version about matching a group of characters occuring 0 or more times).


if it helps I am comparing strings to the patterns. But again I am just seeing if there's a way to discriminate against the extra stuff. Since "dog7 bones and treats" does have a segment that does match my pattern (dog7 bones), but I was wondering if there was a way to say if there's extra after this then it's not a match (the extra being "and treats").

Was it helpful?

Solution

So I have been looking at this and from what I see when dealing with pattern matching it's more like "what matches what I'm hunting for" not "if something doesn't match this EXACTLY then reject". Based on what I've read on MSDN and what I've tried after looking at http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx multiple times I have concluded that for my needs that I will simply have to add additional code to split my strings based on white space and if the length of that split is greater than 2 then I know that there was additional input that I didn't want. Such as

string[] words = myInput.Split(' ');
if(words.Length > 2)
    //ignore this string 

or I can also do the opposite and check if the length is 2 and if so then it's good to do my work with.

I will use the pattern matching to make sure the string inputs are still what they need to be, but I'm going to have to use this additional stuff to discriminate against this extra unwanted stuff.

But again unless someone else knows how to make strings like "dog7 bones and treats" ignorable when I'm looking for things like "AB23454 CD43" this is the solution to my problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top