1

I've been searching for my problem answer, but couldn't find so I write here.

I want to take a string example: = "37513220102304920105590"

and find all matches for numbers of length 11 which starts 3 or 4.

I have been trying to do so:

string input = "37513220102304920105590"
var regex = new Regex("^[3-4][0-9]{10}$");
var matches = regex.Matches(trxPurpose);

// I expect it to have 3 occurances "37513220102", "32201023049" and "30492010559"
// But my matches are empty. 
foreach (Match match in matches)
{
    var number = match.Value;

    // do stuff
}

My question is: Is my regex bad or I do something wrong with mathing?

0

1 Answer 1

3

Use capturing inside a positive lookahead, and you need to remove anchors, too. Note the - between 3 and 4 is redundant.

(?=([34][0-9]{10}))

See the regex demo.

In C#, since the values are captured, you need to collect .Groups[1].Value contents, see C# code:

var s = "37513220102304920105590";
var result = Regex.Matches(s, @"(?=([34][0-9]{10}))")
        .Cast<Match>()
        .Select(m => m.Groups[1].Value)
        .ToList();
Sign up to request clarification or add additional context in comments.

7 Comments

You can write [34] and \d if you want to make your regex shorter ;) I didn't know about the ?=, nice one.
Yes, [3-4] = [34]. It is more a note to OP. As for \d, I would only do that together with adding RegexOptions.ECMAScript flag, since \d matches more than ASCII digits. See \d is less efficient than [0-9].
And \d{10} instead of [0-9]{10}, then I don't think you can make it shorter
"Shorter" in regex does not mean "better". One should know what one is doing when "shortening regex".
I agree. Shortening, it's not only about regex, it's the same about code (and not only). There is something bad about the \d ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.