1

I have a regex to match numbers that are formatted as a phone number or similar to phone numbers. I have to omit the numbers that are start with forward slashes, since that might be a part of a URL. I have used the following regex. It works well in all situations but any number without forward slash in initial point of sentence is ignored by the regex. can anyone help me to fin the mistake here?

/[^\/0-9](\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{2,3}[\s.-]?\d{3}/i

Test Case 1:

23242424 242424242 24242424424 2424244242 242 24242424/1212121212 1 05454545454 5454045454 0115545454 /454545454

Output 1: (Matches indicated in bold italic)

23242424 242424242 24242424424 2424244242 242 24242424/1212121212 1 05454545454 5454045454 0115545454 /454545454

Test Case 2: (a space in front of first number)

23242424 242424242 24242424424 2424244242 242 24242424/1212121212 1 05454545454 5454045454 0115545454 /454545454

Output 2: (Matches indicated in bold italic)

23242424 242424242 24242424424 2424244242 242 24242424/1212121212 1 05454545454 5454045454 0115545454 /454545454

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Fasna
  • 564
  • 1
  • 10
  • 28
  • @anubhava updated – Fasna Aug 24 '20 at 06:23
  • didn't get your question, output here shown is the output i'm receiving now. but I want to match the `23242424` as well. which is currently not happening with the current regex. that's the question – Fasna Aug 24 '20 at 07:25
  • What is max # of digits in phone number because you seem to be matching 11 digit `24242424424` as well. – anubhava Aug 24 '20 at 07:42
  • 2
    May be you can use: https://regex101.com/r/MfMgbz/1 – anubhava Aug 24 '20 at 07:44
  • 2
    You can use `'/(?<![\/0-9])(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{2,3}[\s.-]?\d{3}/'`. See [regex demo](https://regex101.com/r/s2uolx/1). – Wiktor Stribiżew Aug 24 '20 at 07:55
  • @WiktorStribiżew it matches, my requirement. Thanks. – Fasna Aug 24 '20 at 08:10
  • @WiktorStribiżew this expression is not working on Safari browser – Fasna Dec 04 '20 at 12:34
  • @Fasna Correct, not yet. Anyway, it is a question about PHP where the regex library is PCRE. If you need to do the same in JS, you would need a more specific regex that will also depend on the code. – Wiktor Stribiżew Dec 04 '20 at 12:37
  • 1
    @Fasna [Here](https://stackoverflow.com/a/641432/3832970) is how you do it if you want to replace. [Here](https://stackoverflow.com/questions/35142364/regex-negative-lookbehind-not-valid-in-javascript) is a very good post with more alternatives, for matching, too. – Wiktor Stribiżew Dec 04 '20 at 12:38

2 Answers2

2

You can use a negative lookbehind instead of a negated character class to also match start of string positions:

'/(?<![\/0-9])(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{2,3}[\s.-]?\d{3}/'
  ^^^^^^^^^^^^

See the regex demo

Here, (?<![\/0-9]) is a negative lookbehind that matches a location that is not immediately preceded with a forward slash or a digit.

Note you do not need the case insensitive modifier unless this is a part of a longer pattern where you also match letters.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

My way is quite brutal, but i would simply remove any numbers beginning with / from the main string before searching for numbers. Something like $str = preg_replace('/\/\d+/', '', $str);

MarvinLeRouge
  • 444
  • 5
  • 15