If I have a string of data with numbers in it. This pattern is not consistent. I would like to extract all numbers from the string and only a character that is defined as allowed. I thought RegEx might be the easiest way of doing this. Could you provide a regex patter that may do this as I think regex is voodoo and only regex medicine men know how it works
eg/
"Q1W2EE3R45T" = "12345"
"WWED456J" = "456"
"ABC123" = "123"
"N123" = "N123" //N is an allowed character
UPDATE: Here is my code:
var data = Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
data = data.Select(x => Regex.Replace(x, "??????", String.Empty)).ToArray();
No need to use regexes! Just look through the characters and ask each of them whether they are digits.
Or if you need it as a string
EDIT Apparently you also need
'N'
:EDIT EDIT Example:
That's kinda horrible -- nested lambdas -- so you might be better off using the regex for clarity.
Using
Regex.Replace(string,string,string)
static method.Sample
To allow
N
you can change the pattern to[^\dN]
. If you're looking forn
as well you can either applyRegexOptions.IgnoreCase
or change the class to[^\dnN]
How about something along the lines of