C
C#•3w ago
mwc

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
mg
mg•3w ago
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]
mwc
mwcOP•3w ago
Yes. Part of my problem is I'm not entirely sure on the correct terms to use when trying to search for info
mg
mg•3w ago
what's your application for this?
ero
ero•3w ago
are the values always in that exact format? can they ever start with 0?
mwc
mwcOP•3w ago
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.
ero
ero•3w ago
wait so why not just a dictionary or something like that? string as the keys, and whatever type your value is for the values
mwc
mwcOP•3w ago
The values are part of a more detailed class, so I need to find the corresponding objects
ero
ero•3w ago
doesn't really answer the question at all
mwc
mwcOP•3w ago
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.
ero
ero•3w ago
list.First(item => item.Property == "1A")?
mwc
mwcOP•3w ago
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
ero
ero•3w ago
list.Where(item => item.Property == "1A")
mwc
mwcOP•3w ago
Thanks. I was overthinking this, and trying to be smart, but sometimes KISS should be applied 🙂

Did you find this page helpful?