简体   繁体   中英

Match part of a string with regex

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.

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 .

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.

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, eg 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).

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