Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - NSString regex match

I have a string for example:

NSString *str = @"Strängnäs"

Then I use a method for replace scandinavian letters with *, so it would be:

NSString *strReplaced = @"Str*ngn*s"

I need a function to match str with strReplaced. In other words, the * should be treated as any character ( * should match with any character).

How can I achieve this?

Strängnäs should be equal to Str*ngn*s

EDIT:

Maybe I wasn't clear enough. I want * to be treated as any character. So when doing [@"Strängnäs" isEqualToString:@"Str*ngn*s"] it should return YES

like image 274
Carnal Avatar asked Jun 26 '26 20:06

Carnal


1 Answers

I think the following regex pattern will match all non-ASCII text considering that Scandinavian letters are not ASCII:

[^ -~]

Treat each line separately to avoid matching the newline character and replace the matches with *.

Demo: https://regex101.com/r/dI6zN5/1

Edit:

Here's an optimized pattern based on the above one:

[^\000-~]

Demo: https://regex101.com/r/lO0bE9/1

Edit 1: As per your comment, you need a UDF (User defined function) that:

  • takes in the Scandinavian string
  • converts all of its Scandinavian letters to *
  • takes in the string with the asterisks
  • compares the two strings
  • return True if the two strings match, else false.

You can then use the UDF like CompareString(ScanStr,AsteriskStr).

like image 95
Amen Jlili Avatar answered Jun 28 '26 09:06

Amen Jlili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!