简体   繁体   中英

Capturing multiple phone numbers with regex

I'm trying to improve with regex as I'm tired of constantly having to look up existing solutions instead of creating my own. Having a bit of difficulty understanding why this isn't working though:

Trying to extract both phone numbers from the following string (numbers and address are random):

+1-541-754-3010 156 Alphand_St. <J Steeve>\n 133, Green, Rd. <E Kustur> NY-56423 ;+1-541-914-3010\n"

So I'm using the following expression:

 /\+(.+)(?:\s|\b)/

These are the matches I'm getting back:

  1. 1-541-754-3010 156 Alphand_St.
  2. 1-541-914-3010

So I'm getting the last one correctly, but not the first one. Based on the expression, it should match anything from between a + and a space/boundary. But for some reason it's not stopping at the space after the first number. Am I going about this the wrong way?

In the format you provided for the search string, and since you are starting with a literal "+", I would just include the next following string of decimals and separators, like the hyphen:

/\+([0-9\-]+)/

Your ".+" says to match everything until there's a \\s. However that also includes \\s on the way to the \\s.

Remember that dashes - are not word characters, so \\b will match between, for example, 1- and -5 and so on. Also, your current regex is greedy - it'll try to match as many characters as it can with the repeated . , which is why it goes all the way to the end of the first line (because after the last character in the line matches \\b ). Making it lazy (with .+? ) wouldn't fix it, though, because then it would terminate right after the 1 in 1-541 (because between 1- is a word boundary)

Try using a character set of digits and - instead:

\+([\d-]+)

https://regex101.com/r/ktbcHJ/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM