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?

PsiX
  • 1,661
  • 1
  • 17
  • 35
K V
  • 578
  • 2
  • 5
  • 24

1 Answers1

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();
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You can write `[34]` and `\d` if you want to make your regex shorter ;) I didn't know about the `?=`, nice one. – romain-aga Oct 12 '16 at 09:40
  • 2
    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]`*](http://stackoverflow.com/a/16621778/3832970). – Wiktor Stribiżew Oct 12 '16 at 09:41
  • And `\d{10}` instead of `[0-9]{10}`, then I don't think you can make it shorter – romain-aga Oct 12 '16 at 09:45
  • "Shorter" in regex does not mean "better". One should know what one is doing when "shortening regex". – Wiktor Stribiżew Oct 12 '16 at 09:46
  • 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` ? – romain-aga Oct 12 '16 at 09:48
  • @romain-aga: Please check my first comment above (the second comment). There is a link and a lot depends on whether OP needs to match Hindi, Arabic, Hebrew, etc. digits or not. – Wiktor Stribiżew Oct 12 '16 at 09:51
  • Thanks, I didn't know that :) Well, I didn't use it often , but now, I will even use it less, if it's not at all. The documentation should talk about that, it can be misleading to don't be aware this behavior after reading the doc. – romain-aga Oct 12 '16 at 09:54