0

I have following 1010159552597 and I would like to find the numbers that start with 10, followed by 1 or 0 and ending with 7 digits. I use following RegEx to search

(10[01][0-9]{7})

Following result is given: 1010159552

But I also would have expected the following: 1015955259

How can I manage to get both results?

Thanks

Korrie Soab
  • 15
  • 1
  • 3
  • So do you have a list of numbers or a big string with one long number in it or is this a user input and you just want to test if the typed number matches? What about decimals `101.007`? – Erik Philips Jan 16 '18 at 16:33
  • Do you need to return whether a given number is valid or not based out of the regex expression? – Maddy Jan 16 '18 at 16:35

2 Answers2

1

Regular expressions consume characters and don't go back over previous matches. A way around this is to use zero-length assertions (see code below) to capture what you want.

Code

See regex in use here

(?=(10[01]\d{7}))

Results are in capture group 1:

  • 1010159552
  • 1015955259

Explanation

  • (?=(10[01]\d{7})) Positive lookahead ensuring what follows matches
    • (10[01]\d{7}) Capture your original expression into capture group 1
ctwheels
  • 21,901
  • 9
  • 42
  • 77
0

You're right in that your expectation does match your regex, however, it will try to find the first instance of that match.

In your case the first term is:

10 - 1 - 0159552

so this is the solution given.

Since your results are overlapping, you might want to check out this article.

Overlapping matches in Regex

Kevin C
  • 1
  • 1