Swift Replace Substring Regex

Swift replace substring regex

You need to remove the ^ and $ anchors.

The ^ means start of string and $ means end of string (or line, depending on the options). That's why your first example works: in the first test string, the start of the string is really followed by your pattern and ends with it.

In the second test string, the pattern is found in the middle of the string, thus the ^... can't apply. If you would just remove the ^, the $ would apply on the second occurrence of the registration number and the output would be my car reg 1 - DD11 AAA my car reg 2 - XX.

let myString = "my car reg 1 - DD11 AAA  my car reg 2 - AA22 BBB"
let regex = try! NSRegularExpression(pattern: "([A-HK-PRSVWY][A-HJ-PR-Y])\\s?([0][2-9]|[1-9][0-9])\\s?[A-HJ-PR-Z]{3}", options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, myString.count)
let modString = regex.stringByReplacingMatches(in: myString, options: [], range: range, withTemplate: "XX")
print(modString)
// Output: "my car reg 1 - XX my car reg 2 - XX"

Replace string with regex in swift

In Swift, you can use stringByReplacingMatchesInString for a regex-based replace.

Here is a snippet showing how to use it:

let txt = "myname@test.com"
let regex = NSRegularExpression(pattern: "([^@\\s]+)@test\\.(\\w+)", options:nil, error: nil)
let newString = regex!.stringByReplacingMatchesInString(txt, options: nil, range: NSMakeRange(0, count(txt)), withTemplate: "$1@hoge.$2")
println(newString)

Swift 4.2 update:

let txt = "myname@test.com"
let regex = "([^@\\s]+)@test\\.(\\w+)"
let repl = "$1@hoge.$2"
print( txt.replacingOccurrences(of: regex, with: repl, options: [.regularExpression]) )

Note that I changed the regex to

  • ([^@\\s]+) - matches 1 or more characters other than @ or whitespace
  • @ - matches @ literally
  • test\\.(\\w+) - matches test. literally and then 1 or more alphanumeric character (\w+).

Note that in the replacement string, you do not need to escape the period.

Replace matches using regex by modifying matched string in swift

You may use

var value: NSMutableString = "It is live now at Germany(DE) or (SOFE)"
let pattern = "(?<=\\G(?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(?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 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.

Regex pattern match and replace in Swift

Your pattern is wrong, you have at the start [a-z], so you aren't detecting anything.

Also, prefers utf16 count with NSStuff (because with NSString, it's UTF16)

let myString = "Hi this is %1$s, product %2$s Hi this is %2$s, product %2$s"

let range = NSRange(location: 0, length: myString.utf16.count)
var regex = try! NSRegularExpression(pattern: "%(\\d+)\\$s", options: [])
var newStr = regex.stringByReplacingMatches(in: myString, options: [], range: range, withTemplate: "{$1}")
print(newStr)

Output:

$>Hi this is {1}, product {2} Hi this is {2}, product {2}

Some explanation on %(\d+)\$s (and then redo a \ for Swift strings).

% : Detect "%"

\d+: Detect number (including 12 which weren't your case before)

(\d+): Detect number, but in a capture group

\$: Detect "$" (need an escape because it's a special char in regex)

s: Detect "s"

So there are two groups: The whole (corresponding to the whole regex match), and the numbers. The first one would be $0, and the second one $1, that's why I used {$1} in the template.

NB: I used https://regex101.com to check the pattern.

With increment, you can't do it with the template. You have to enumerate all the matches, do the operation and replace.

var myString = "Hi this is %1$s, product %2$s Hi this is %2$s, product %2$s"
let range = NSRange(location: 0, length: myString.utf16.count)
var regex = try! NSRegularExpression(pattern: "%(\\d+)\\$s", options: [])
let matches = regex.matches(in: myString, options: [] , range: range)
matches.reversed().forEach({ aMatch in
let fullNSRange = aMatch.range
guard let fullRange = Range(fullNSRange, in: myString) else { return }
let subNSRange = aMatch.range(at: 1)
guard let subRange = Range(subNSRange, in: myString) else { return }
let subString = myString[subRange]
guard let subInt = Int(subString) else { return }
let replacement = "{" + String(subInt + 1) + "}"
myString.replaceSubrange(fullRange, with: replacement)
})

Swift - Replace exact matching Characters / Words in String (not containing)

You just need to use replacingOccurences(of:with:options:) and pass .regularExpression to options. You also need to pass a regex to of: now instead of just passing the substring you want to replace. The correct regex here if \\bc\\b, which matches a word boundary before and after c ensuring that it's just the char c you are matching and not a c that's part of a word/expression.

let string = "c \\cdot c"
let replacingString = string.replacingOccurrences(of: "\\bc\\b", with: "2", options: .regularExpression)
print(replacingString) // "2 \\cdot 2"

Replace in string with regex

You can try NSRegularExpression. Something like:

import Foundation

var sourceStr = "Hello, he is hero"
let regex = try! NSRegularExpression(pattern: "(he)")
let matches = regex.matches(in: sourceStr, range: NSRange(sourceStr.startIndex..., in: sourceStr))
regex.enumerateMatches(in: sourceStr, range: NSRange(sourceStr.startIndex..., in: sourceStr)) { (match, _, _) in
guard let match = match else { return }
guard let range = Range(match.range, in: sourceStr) else { return }
let sub = sourceStr[range]
sourceStr = sourceStr.replacingOccurrences(of: sub, with: sub.uppercased(), options: [], range: range)
}

print(sourceStr)

Replacing a string with a capture group in Swift 3

You need to replace the whole match with the $1 replacement backreference that holds the contents captured with the first capturing group.

Besides, I'd advise to write the [\d] as \d to avoid misinterpretations of the character class unions if you decide to expand the pattern later. Also, it is safer to use .*?, lazy dot matching, to get to the first ] rather than to the last ] (if you use .* greedy variation). However, that depends on the real requirements.

Use either of the following:

let txt = "Lorem ipsum @[1484987415095898:274:Page Four] dolores"
let regex = NSRegularExpression(pattern: "@\\[\\d+:\\d+:(.*?)\\]", options:nil, error: nil)
let newString = regex!.stringByReplacingMatchesInString(txt, options: nil, range: NSMakeRange(0, count(txt)), withTemplate: "$1")
print(newString)
// => Lorem ipsum Page Four dolores

or

let txt = "Lorem ipsum @[1484987415095898:274:Page Four] dolores"
let newString = txt.replacingOccurrences(of: "@\\[\\d+:\\d+:(.*?)]", with: "$1", options: .regularExpression)
print(newString)
// => Lorem ipsum Page Four dolores

Replace regex \\+n from string in swift

To remove all ns that contain one or more backslashes in front, and all newlines with any amount of backslashes in front, you can use

#"\\+n|\\*\n"#

Note the # before and after the double quotes mean that the literal is a raw string literal where backslashes are treated as literal backslashes and do not form string escape sequences, like \n, \t, \r, etc.

Here, the pattern means

  • \\+n - one or more backslashes followed with n
  • | - or
  • \\*\n - zero or more backslashes followed with a newline.

Note that here, \n is a regex escape matching a newline, it is not an LF, \x0A character.

Replace occurrences swift regex

In your pattern, [.#$[/]], there is a character class union, that is, it only matches ., #, $ and / characters (a combination of two character classes, [.#$] and [/]).

In ICU regex, you need to escape literal square brackets [ and ] inside a character class:

"[.#$\\[/\\]]"

This code outputs me,ow@gmai,l,co,m:

func cleanStr(str: String) -> String {
return str.replacingOccurrences(of: "[.#$\\[/\\]]", with: ",", options: [.regularExpression])
}
print(cleanStr(str: "me[ow@gmai#l.co$m"))

Replace matching regex values in string with correct value from dictionary

Here's one solution that works. The string handling is even more complicated because you also have to deal with NSRange.

extension String {
func format(with parameters: [String: Any]) -> String {
var result = self

//Handles keys with letters, numbers, underscore, and hyphen
let regex = try! NSRegularExpression(pattern: "\\{([-A-Za-z0-9_]*)\\}", options: [])

// Get all of the matching keys in the curly braces
let matches = regex.matches(in: self, options: [], range: NSRange(self.startIndex..
// Iterate in reverse to avoid messing up the ranges as the keys are replaced with the values
for match in matches.reversed() {
// Make sure there are two matches each
// range 0 includes the curly braces
// range 1 includes just the key name in the curly braces
if match.numberOfRanges == 2 {
// Make sure the ranges are valid (this should never fail)
if let keyRange = Range(match.range(at: 1), in: self), let fullRange = Range(match.range(at: 0), in: self) {
// Get the key in the curly braces
let key = String(self[keyRange])
// Get that value from the dictionary
if let val = parameters[key] {
result.replaceSubrange(fullRange, with: "\(val)")
}
}
}
}

return result
}
}

var text = "the {animal} jumped over the {description} fox"
var dictionary = ["animal":"dog" , "description": "jumped"]
print(text.format(with: dictionary))

Output:

the dog jumped over the jumped fox

This code leaves the original {keyname} in the string if it's not found in the dictionary. Adjust that code as desired.



Related Topics



Leave a reply



Submit