3

Im trying to replace matched strings using regex in swift, my requirement is as below

originalString = "It is live now at Germany(DE)"

i want the string within the (" ") i.eDE to be separated by space i.e. "D E"

so replacedString should be "It is live now at Germany(D E)"

i tried below code

var value: NSMutableString = "It is live now at Germany(DE)"
let pattern = "(\\([A-Za-z ]+\\))"
let regex = try? NSRegularExpression(pattern: pattern)
regex?.replaceMatches(in: value, options: .reportProgress, range: 
NSRange(location: 0,length: value.length), withTemplate: " $1 ")
print(value)

output is It is live now at Germany (DE), i know it's not what is required. here it is based on the template where we cannot modify based on matched string value. Is there any way to achieve this ?

Thanks in advance

4
  • Is the format fixed as (+2 letters+)? Try value.replacingOccurrences(of: "(\\([A-Za-z])([A-Za-z]\\))", with: "$1 $2", options: String.CompareOptions.regularExpression, range: nil) Commented Aug 6, 2019 at 7:43
  • @Wiktor, thanks for your response. No it is not fixed with 2 letters it can be more than 2 as well. Commented Aug 6, 2019 at 7:51
  • Ok, so that is always (, then 1 or more spaces or letters and then a ), right? Commented Aug 6, 2019 at 7:52
  • Yes, just to give another example "(SOFE)" should be "(S O F E)" Commented Aug 6, 2019 at 7:54

1 Answer 1

2

You may use

var value: NSMutableString = "It is live now at Germany(DE) or (SOFE)"
let pattern = "(?<=\\G(?<!\\A)|\\()[A-Za-z](?=[A-Za-z]+\\))"
let regex = try? NSRegularExpression(pattern: pattern)
regex?.replaceMatches(in: value, options: .reportProgress, range: NSRange(location: 0,length: value.length), withTemplate: "$0 ")
print(value)

Or just

let val =  "It is live now at Germany(DE) or (SOFE)"
let pattern = "(?<=\\G(?<!\\A)|\\()[A-Za-z](?=[A-Za-z]+\\))"
print( val.replacingOccurrences(of: pattern, with: "$0 ", options: .regularExpression, range: nil) )

Output: It is live now at Germany(D E) or (S O F E)

Pattern details

  • (?<=\\G(?<!\\A)|\\() - a positive lookbehind that matches a location right after ( or at the end of the preceding successful match
  • [A-Za-z] - matches and consumes any ASCII letter
  • (?=[A-Za-z]+\\)) - a positive lookahead that matches a location that is immediately followed with 1+ ASCII letters and then a ) char.

The $0 in the replacement inserts the whole match value back into the resulting string.

Sign up to request clarification or add additional context in comments.

2 Comments

It works. But i have a query, which part of the regex is adding space " " between the characters?. Sorry if it is a silly question as my understanding of RegEx is that of a beginner
@RajeshRs withTemplate: "$0 " / with: "$0 " - do you see the space now? Regex never inserts or add anything, regex patterns are used to search for strings meeting some pattern. The replacement patterns are used to add/insert chars to/instead of matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.