Regex - Extract phone numbers from string

后端 未结 1 1454
不思量自难忘°
不思量自难忘° 2021-01-23 08:23

I need to extract two phone numbers from a string with delimiters (tilde). The tricky part is the format of the phone numbers can vary.

The string pattern stays the same

相关标签:
1条回答
  • 2021-01-23 09:01

    You may use

    (?<=~\+|~)([0-9]+)(?=~)
    

    See the regex demo

    If there is a problem with lookbehind, use a bit modified variant:

    (?:(?<=~\+)|(?<=~))([0-9]+)(?=~)
    

    Details

    • (?<=~\+|~) - there must be ~+ or ~ immediately to the left of the current location
    • ([0-9]+) - Group 1: one or more digits
    • (?=~) - there must be ~ immediately to the right of the current location.
    0 讨论(0)
提交回复
热议问题