1

I want to match the right phone number from a string. For example:

string := "my phone number is 15817452367"
re := regexp.MustCompile(`1[34578][0-9]{9}`)
re.FindAllString(string, -1)
-> 15817452367

This result is right, but for another string:

string := "my card number is 115817452367"
re := regexp.MustCompile(`1[34578][0-9]{9}`)
re.FindAllString(string, -1)
-> 15817452367 (invalid)

This regex also matches the phone number, but I want this to not be a valid value,I don't want to get a phone number form a digital string. How can I fix the regex? Thanks in advance!

pangpang
  • 8,581
  • 11
  • 60
  • 96
  • 2
    I'm trying to understand your question. Maybe you want to use `^1[34578][0-9]{9}$` to indicate the begging and end of the string? Or `(?:^|[^0-9])1[34578][0-9]{9}(?:$|[^0-9])` – Ari Seyhun Sep 07 '17 at 15:57
  • 1
    What comes before he number? You need to qualify the start of the string somehow, which may just mean `^`. It's always "safest" to anchor your regexes at one or both ends if you're trying to get an exact match. – JimB Sep 07 '17 at 16:01
  • @Acidic the second is what I want – pangpang Sep 07 '17 at 16:06
  • Ah hopefully that regex works well. I set the begging to check for `start of text OR a non-digit character` that **does not** capture. Same with the end of the string. So you should be left with just the phone number! – Ari Seyhun Sep 07 '17 at 16:08
  • 2
    Not sure how this isn't a duplicate for something like [Regular expression to match standard 10 digit phone number](https://stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number). If you have a number format, but sometimes that number format can be broken, then why use that number format? You can try maybe prepending `1?` to the regular expression otherwise just use something like `\b\d{9,}\b` ensuring the minimum/maximum requirements are met for your phone number formatting – ctwheels Sep 07 '17 at 16:11
  • @pangpang Wait a second, are you trying to match the first string but not the second? If so just add `\b` before and after your regex and you'll be fine. `\b1[34578]\d{9}\b` – ctwheels Sep 07 '17 at 16:39
  • @ctwheels Thanks a lot, It is a good solution! – pangpang Sep 07 '17 at 16:44
  • You're very welcome! `\b` is a word boundary. It is used to match (without consuming any characters) between a `\w` and a non `\w` (between `[a-zA-Z0-9_]` and `[^a-zA-Z0-9_]`). For more information about regular expression tokens, check out [regex101](https://regex101.com). They have fantastic documentation, regex explanation and testing available online. – ctwheels Sep 07 '17 at 18:45

2 Answers2

3

You can check the beginning and end of the regex for the beginning/end of the string or a non-digit character and only capture the phone number.

Here is some working Regex as an example:

(?:^|[^0-9])(1[34578][0-9]{9})(?:$|[^0-9])

In Golang

re := regexp.MustCompile(`(?:^|[^0-9])(1[34578][0-9]{9})(?:$|[^0-9])`)
submatch := re.FindStringSubmatch("Phone Number: 15817452367;")
if len(submatch) < 2 {
    // No match found
}
match := submatch[1]

Explanation:

Find the start of the string or a non-digit character. Enclosed in non-capture group.

(?:^|[^0-9])

Find the phone number you are searching for. Enclosed in capture group.

(1[34578][0-9]{9})

Find the end of the string or a non-digit character. Enclosed in non-capture group.

(?:$|[^0-9])
Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
0

You can check if the matched string is not preceded nor followed by digits. In order to check this, nest your pattern between two [^0-9]*: it means "nothing or not a digit".

regexp.MustCompile(`[^0-9]*1[34578][0-9]{9}[^0-9]*`)

This way, 15817452367 will be detected as a match, but 115817452367 will not.

Right leg
  • 16,080
  • 7
  • 48
  • 81