Regex to match on full number only
Not really C# specific, but it is in a C# app that I'm working on.
I've got a List of Strings that contains various values in a consistent format (one or two digit number, followed by a single letter), that I need to search on the full number only.
Some example values are 1A,1B,10A,10B.
What I'd like to do is return matches on the full number only, so using the example data, if I search for 1, I'd only want 1A, and 1B as the result.
Off course, if there's an alternative to use regex to achieve this that I'm not aware off, then I'd be open to knowing about it.
13 Replies
i don't understand what you mean by full number
you mean you want "1" to not match "10"?
in that case you can use
1[^0-9]
Yes. Part of my problem is I'm not entirely sure on the correct terms to use when trying to search for info
what's your application for this?
are the values always in that exact format?
can they ever start with 0?
The application is I have a list of hardware inputs that have an A and B channel. So I want to able to search for a specific channel number, and return the corresponding inputs.
Yes. Either one or two digit numbers, followed by a single letter
Yes.
wait so why not just a dictionary or something like that?
string as the keys, and whatever type your value is for the values
The values are part of a more detailed class, so I need to find the corresponding objects
doesn't really answer the question at all
I have a List of a class that has a property containing the value. I need to be able to search that list, based on the value.
list.First(item => item.Property == "1A")
?The other option is I could do two seperate text searches for A and B
Like that, but there may be duplicates, however I can handle that
list.Where(item => item.Property == "1A")
Thanks.
I was overthinking this, and trying to be smart, but sometimes KISS should be applied 🙂