Skip to content

Regex Help #65839

Answered by stephentoub
TonyValenti asked this question in Q&A
Discussion options

You must be logged in to vote

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.

Is that possible to do solely with regex or do I need to do named captures and combine them in code?

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();
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by TonyValenti
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants