-1

I have two arrays of strings and I want to check if a string of array a matches a string from array b. Those strings are phone numbers that might come in different formats. For example:

Array a might have a phone number with prefix like so +44123123123 or 0044123123123

Array b have a standard format without prefixes like so 123123123

So I'm looking for a regex that can match a part of a string like +44123123123 with 123123123

Btw I'm using Swift but I don't think there's a native way to do it (at least a more straightforward solution)

EDIT

I decided to reactivate the question after experimenting with the library @Larme mentioned because of inconsistent results.

I'd prefer a simper solution as I've stated earlier.

SOLUTION

Thanks guys for the responses. I saw many comments saying that Regex is not the right solution for this problem. And this is partly true. It could be true (or false) depending on my current setup/architecture ( which thinking about it now I realise that I should've explained better). So I ended up using the native solution (hasSuffix/contains) but to do that I had to do some refactoring on the way the entire flow was structured. In the end I think it was the least complicated solution and more performant of the two. I'll give the bounty to @Alexey Inkin for being the first to mention the native solution and the right answer to @Ωmega for providing a more complete solution.

grenos
  • 307
  • 1
  • 6
  • 18
  • Not tested, but https://github.com/marmelroy/PhoneNumberKit seems interesting. I'd format all your numbers into the same style, and compare. – Larme Mar 17 '21 at 20:10
  • @Larme Yeah I already use it but their regex matches specific numbers with specific prefixes from different regions (and not always without errors). So you can imagine it takes a while to do all the necessary checks. I'm looking for a faster solution (by excluding prefixes altogether from the checks) – grenos Mar 17 '21 at 20:12
  • @Larme Sorry I just re-read the comment. I didn't think of using their formatters. That might do the trick. I'll have a look. Thanks – grenos Mar 17 '21 at 20:21
  • @Larme yep I can parse the number and get only the "national number" which is basically the number without the prefix – grenos Mar 17 '21 at 20:33
  • 1
    If you just want to see if it matches, why not just use index of or string contains kind of methods. Is there any other requirement too? – T.kowshik Yedida Mar 20 '21 at 04:08
  • 1
    have you tried this? https://stackoverflow.com/questions/24034043/how-do-i-check-if-a-string-contains-another-string-in-swift – eamanola Mar 20 '21 at 04:18
  • that'll be a long regex-mask to extract these numbers, – Heo Mar 22 '21 at 04:44
  • The solution to your problem is normalization, not Regex. If you need to compare two phone numbers you bring them both into normalized form and compare those. – Martin Mar 22 '21 at 15:18

3 Answers3

1

I believe regex is not the right approach for this task.

Instead, you should do something like this:

var c : [String] = b.filter ({ (short : String) -> Bool in
  var result = false
  for full in a {
    result = result || full.hasSuffix(short)
  }
  return result
})

Check this demo.


...or similar solution like this:

var c : [String] = b.filter ({ (short : String) -> Bool in
  for full in a {
    if full.hasSuffix(short) { return true }
  }
  return false
})

Check this demo.

Ωmega
  • 42,614
  • 34
  • 134
  • 203
0

As you do not mention requirements to prefixes, the simplest solution is to check if string in a ends with a string in b. For this, take a look at https://developer.apple.com/documentation/swift/string/1541149-hassuffix

Then, if you have to check if the prefix belongs to a country, you may replace ^00 with + and then run a whitelist check against known prefixes. And the prefix itself can be obtained as a substring by cutting b's length of characters. Not really a regex's job.

Alexey Inkin
  • 1,853
  • 1
  • 12
  • 32
0

I agree with Alexey Inkin that this can also nicely be solved without regex. If you really want a regex, you can try something like the following:

(?:(\+|00)(93|355|213|1684|376))?(\d+)
          ^^^^^^^^^^^^^^^^^^^^^ Add here all your expected country prefixes (see below)
^^^                            ^^ Match a country prefix if it exists but don't give it a group number
   ^^^^^^^ Match the "prefix-prefix" (+ or 00)
                                 ^^^^ Match the local phone number

Unfortunatly with this regex, you have to provide all the expected country prefixes. But you can surely get this list online, e.g. here: https://www.countrycode.org

With this regex above you will get the local phone number in matching group 3 (and the "prefix-prefix" in group 1 and the country code in group 2).

Itchy
  • 2,263
  • 28
  • 41