Regex Help #65839
-
I have inputs that looks like this:
I am interested in capturing |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Questions like this are better for a site like stackoverflow. The dotnet/runtime repo isn't intended to be a general Q&A destination for how to use .NET. Thanks.
There's no mechanism in Regex to post-process captures as part of the match itself. You can have a capture for the whole region, though, and then just remove the spaces yourself, e.g. Regex r = new Regex(@"\w{3}\s*-\s*\d{3}"); // I'm assuming your pattern is something like this
...
Match m = r.Match(input);
while (m.Success)
{
string valueWithSpaces = m.Value.Replace(" ", "");
Use(valueWithSpaces);
m = m.NextMatch();
} or as you say have individual capture groups that you then combine: Regex r = new Regex(@"(\w{3})\s*-\s*(\d{3})");
...
Match m = r.Match(input);
while (m.Success)
{
string valueWithSpaces = string.Concat(m.Groups[1].ValueSpan, "-", m.Groups[2].ValueSpan);
Use(valueWithSpaces);
m = m.NextMatch();
} |
Beta Was this translation helpful? Give feedback.
Questions like this are better for a site like stackoverflow. The dotnet/runtime repo isn't intended to be a general Q&A destination for how to use .NET. Thanks.
There's no mechanism in Regex to post-process captures as part of the match itself. You can have a capture for the whole region, though, and then just remove the spaces yourself, e.g.