Reputation: 141
I have a situation..
Having a string which can contain numbers,letters, and some symbols, I want to make an extraction from it or to make some replacements based on some "rules".
I thing the best is to give some examples of possible situations and what I want to do (display):
String Display1 or Display2
AB_X345 X345 or ###X345
AB_1234 1234 or ###1234
X987_TEXT_4567 X9874567 or X987######4567
X987TEXT4567 X9874567 or X987####4567
X798TEXT X798 or X798####
789TEXT 789 or 789####
X400 X400 or X400
So practically when I find an X followed by numbers I want to display them. If some text appear, I don't want it displayed or I want it masked with a character(#). In case no X is present, I want to display only the numbers. Is Regex the easyest way of doing this? (I am not familiar with regex-just had a bird's eye view on it). Can all the rules be gathered in a single regex expression or is to complicated?
Thank you for any sugestions
Upvotes: 1
Views: 1965
Reputation: 111870
Try this for Display 1: @"(?<![A-Za-z])X[0-9]+|[0-9]+"
var rx = new Regex(@"(?<![A-Za-z])X[0-9]+|[0-9]+");
var matches = rx.Matches("X987_TEXT_4567");
var result = "";
foreach (Match match in matches)
{
result += match.Value;
}
Under C# 4.0 you can even do
var rx = new Regex(@"(?<![A-Za-z])(?<1>X[0-9]+)?(?:(?:[^0-9]*)(?<1>[0-9]+))*");
var match = rx.Match("X987_TEXT_4567_123");
var res = string.Concat(match.Groups[1].Captures.OfType<Capture>().Select(p => p.Value));
But the regex at this point becomes a little unreadable :-)
Upvotes: 1
Reputation: 6302
Try this one, check the example below and test it.
\d?X[0-9]+|[0-9]
Example:
http://rubular.com/r/cA5Y49pCtV
Upvotes: 0
Reputation: 18064
Try this regex:
X\d|\d
OR
/X\d|\d/g
This will select only digits or digit starts with 'X'
Upvotes: 1
Reputation: 336198
That's easy:
resultString = Regex.Replace(subjectString,
@"\D # Match a non-digit character
(?<! # unless...
X # it's an X
(?=\d) # which is followed by a digit.
) # End of lookbehind",
"", RegexOptions.IgnorePatternWhitespace);
Change the last line to
"#", RegexOptions.IgnorePatternWhitespace);
to mask the characters with #
instead of removing them.
Upvotes: 2