Regular expression with backslash in swift - swift

i am having problems using replacingOccurrences to replace a word after some specific keywords inside a textview in swift 5 and Xcode 12.
For example:
My textview will have the following string "NAME\JOHN PHONE\555444333"
"NAME" and "PHONE" will be unique so anytime i change the proper field i want to change the name or phone inside this textview.
let's for example change JOHN for CLOE with the code
txtOther.text = txtOther.text.replacingOccurrences(of: "NAME(.*?)\\s", with: "NAME\\\(new_value) ", options: .regularExpression)
print (string)
output: "NAMECLOE " instead of "NAME\CLOE "
I can't get the backslash to get print according to the regular expression.
Or maybe change the regex expression just to change JOHN for CLOE after "NAME"
Thanks!!!
Ariel

You can solve this by using a raw string for your regular expresion, that is a string surrounded with #
let pattern = #"(NAME\\)(.*)\s"#
Note that name and the \ is in a separate group that can be referenced when replacing
let output = string.replacingOccurrences(of: pattern, with: "$1\(new_value) ", options: .regularExpression)

Use
"NAME\\JOHN PHONE\\555444333".replacingOccurrences(
of: #"NAME\\(\S+)"#,
with: "NAME\\\\\(new_value)",
options: .regularExpression
)
Double backslashes in the replacement, backslash is a special metacharacter inside a replacement.
\S+ matches one or more characters different from whitespace, this is shorter and more efficient than .*?\s, and you do not have to worry about how to put back the whitespace.

Related

How do you find all first letter capitalized words in a string? Like a name ("My Name and another His Name")?

GOAL: I want to have a regex that searches a string for all capitalized first letters of words/names in a string. Then replace those capitalized words with "Whatever", if the word is 4 or more characters long. Then replace words 4 or more letters that are lowercased with "whatever".
String Input:
let myString = "This is a regular string. How does this Work? Does any Name know How?"
Desired String Output:
let myString = "Whatever is a whatever whatever. How whatever this Whatever? Whatever any Whatever whatever whatever How?"
What I've tried:
let myString = "This is a regular string. How does this Work? Does any Name know How?"
let x = myString.replacingOccurrences(of: "\\b\\p{Lu}{4,}\\b",
with: "Whatever",
options: .regularExpression)
let finalX = x.replacingOccurrences(of: "\\b\\p{L}{4,}\\b",
with: "whatever",
options: .regularExpression)
print(finalX)
Problem:
The first check is for capitalized letters, the second is for lowercased letters, but it still returns all lowercased.
Would anyone know how to go about this with what I have?
The regular expression you want is the following:
\b[A-Z][a-z]{3,}\b
That will only work with the basic letters A-Z and a-z. If you want to fully support any alphabet then you would need something like this:
\b\p{Lu}\p{Ll}{3,}\b
The \b means "word boundary". The \p{Lu} means "uppercase letters". The \p{Ll} means "lowercase letters".
You only want to use this in the call to replacingOccurrences. There's little reason to do the initial check using contains. That will be looking for the literal text you pass in and of course the regular expression you are using won't be found as literal text in the string.
let myString = "This is a regular string. How does this Work? Does any Name know How?"
let x = myString.replacingOccurrences(of: "\\b\\p{Lu}\\p{Ll}{3,}\\b",
with: "Whatever",
options: .regularExpression)
print(x)
Output:
Whatever is a regular string. How does this Whatever? Whatever any Whatever know How?
To do both you also need "\\b\\p{Ll}{4,}\\b".
The following changes both sets.
let myString = "This is a regular string. How does this Work? Does any Name know How?"
let x = myString
.replacingOccurrences(of: "\\b\\p{Ll}{4,}\\b",
with: "whatever",
options: .regularExpression)
.replacingOccurrences(of: "\\b\\p{Lu}\\p{Ll}{3,}\\b",
with: "Whatever",
options: .regularExpression)
print(x)
Output:
Whatever is a whatever whatever. How whatever whatever Whatever? Whatever any Whatever whatever How?

Why I cannot use \ or backslash in a String in Swift?

I have a string like this in below and I want replace space with backslash and space.
let test: String = "Hello world".replacingOccurrences(of: " ", with: "\ ")
print(test)
But Xcode make error of :
Invalid escape sequence in literal
The code in up is working for any other character or words, but does not for backslash. Why?
Backslash is used to escape characters. So to print a backslash itself, you need to escape it. Use \\.
For Swift 5 or later you can avoid needing to escape backslashes using the enhanced string delimiters:
let backSlashSpace = #"\ "#
If you need String interpolation as well:
let value = 5
let backSlashSpaceWithValue = #"\\#(value) "#
print(backSlashSpaceWithValue) // \5
You can use as many pound signs as you wish. Just make sure to mach the same amount in you string interpolation:
let value = 5
let backSlashSpaceWithValue = ###"\\###(value) "###
print(backSlashSpaceWithValue) // \5
Note: If you would like more info about this already implemented Swift evolution proposal SE-0200 Enhancing String Literals Delimiters to Support Raw Text

How can I individuate the text that encloses some other text in a pattern and edit it with regex

I'm learning about regex and I'm trying to create a program where a certain pattern is substituted.
Given the following string:
###hello#!
I want to recognise "###" and "#!" and substitute them with "*** and "*^". What's between these characters must remain as it is.
Now, I tried something like:
text.replacingOccurrences(of: #"(###)"#, with: "***", options: .regularExpression)
text.replacingOccurrences(of: #"(#!)"#, with: "*^", options: .regularExpression)
but if my string is:
"###hello#! ###hello###"
my output becomes:
"**hello^ hello"
while the desired one should be:
"**hello^ ###hello###"
In fact I only want the characters to be substituted when they follow the pattern:
### some text #!
I created a regex with the following pattern:
#"(###)(?:\\.*?)(#!)"#
but I'm not able to get the text and substitute it.
How can I individuate the text that encloses some other text in a pattern and edit it?
You can use
text = text.replacingOccurrences(of: #"(?s)###(.*?)#!"#, with: "***$1*^", options: .regularExpression)
See the regex demo. Details:
(?s) - an inline "singleline" flag that makes . match any char
### - left-hand delimiter
(.*?) - Capturing group 1 ($1 refers to this value): any zero or more chars as few as possible
#! - right-hand delimiter.
Swift test:
let text = "###hello#! ###hello###"
print(text.replacingOccurrences(of: #"(?s)###(.*?)#!"#, with: "***$1*^", options: .regularExpression))
// -> ***hello*^ ###hello###

Count leading tabs in Swift string

I need to count the number of leading tabs in a Swift string. I know there are fairly simple solutions (e.g. looping over the string until a non-tab character is encountered) but I am looking for a more elegant solution.
I have attempted to use a regex such as ^\\t* along with the .numberOfMatches method but this detects all the tab characters as one match. For example, if the string has three leading tabs then that method just returns 1. Is there a way to write a regex that treats each individual tab character as a single match?
Also open to other ways of approaching this without using a regex.
Here is a non-regex solution
let count = someString.prefix(while: {$0 == "\t"}).count
You may use
\G\t
See the regex demo.
Here,
\G - matches a string start position or end of the previous match position, and
\t - matches a single tab.
Swift test:
let string = "\t\t123"
let regex = try! NSRegularExpression(pattern: "\\G\t", options: [])
let numberOfOccurrences = regex.numberOfMatches(in: string, range: NSRange(string.startIndex..., in: string))
print(numberOfOccurrences) // => 2

How to remove spaces from a string in Swift?

I have the need to remove leading and trailing spaces around a punctuation character.
For example: Hello , World ... I 'm a newbie iOS Developer.
And I'd like to have: > Hello, World... I'm a newbie iOS Developer.
How can I do this? I tried to get components of the string and enumerate it by sentences. But that is not what I need
Rob's answer is great, but you can trim it down quite a lot by taking advantage of the \p{Po} regular expression class. Getting rid of the spaces around punctuation then becomes a single regular expression replace:
import Foundation
let input = "Hello , World ... I 'm a newbie iOS Developer."
let result = input.replacingOccurrences(of: "\\s*(\\p{Po}\\s?)\\s*",
with: "$1",
options: [.regularExpression])
print(result) // "Hello, World... I'm a newbie iOS Developer."
Rob's answer also tries to trim leading/trailing spaces, but your input doesn't have any of those. If you do care about that you can just call result.trimmingCharacters(in: .whitespacesAndNewlines) on the result.
Here's an explanation for the regular expression. Removing the double-escapes it looks like
\s*(\p{Po}\s?)\s*
This is comprised of the following components:
\s* - Match zero or more whitespace characters (and throw them away)
(…) - Capturing group. Anything inside this group is preserved by the replacement (the $1 in the replacement refers to this group).
\p{Po} - Match a single character in the "Other_Punctuation" unicode category. This includes things like ., ', and …, but excludes things like ( or -.
\s? - Match a single optional whitespace character. This preserves the space after periods (or ellipses).
\s* - Once again, match zero or more whitespace characters (and throw them away). This is what turns your , World into , World.
For Swift 3 or 4 you can use :
let trimmedString = string.trimmingCharacters(in: .whitespaces)
This is a really wonderful problem and a shame that it isn't easier to do in Swift today (someday it will be, but not today).
I kind of hate this code, but I'm getting on a plane for 20 hours, and don't have time to make it nicer. This may at least get you started using NSMutableString. It'd be nice to work in String, and Swift hates regular expressions, so this is kind of hideous, but at least it's a start.
import Foundation
let input = "Hello, World ... I 'm a newbie iOS Developer."
let adjustments = [
(pattern: "\\s*(\\.\\.\\.|\\.|,)\\s*", replacement: "$1 "), // elipsis or period or comma has trailing space
(pattern: "\\s*'\\s*", replacement: "'"), // apostrophe has no extra space
(pattern: "^\\s+|\\s+$", replacement: ""), // remove leading or trailing space
]
let mutableString = NSMutableString(string: input)
for (pattern, replacement) in adjustments {
let re = try! NSRegularExpression(pattern: pattern)
re.replaceMatches(in: mutableString,
options: [],
range: NSRange(location: 0, length: mutableString.length),
withTemplate: replacement)
}
mutableString // "Hello, World... I'm a newbie iOS Developer."
Regular expressions can be very confusing when you first encounter them. A few hints at reading these:
The specific language Foundation uses is described by ICU.
Backslash (\) means "the next character is special" for a regex. But inside a Swift string, backslash means "the next character is special" of the string. So you have to double them all.
\s means "a whitespace character"
\s* means "zero or more whitespace characters"
\s+ means "one or more whitespace characters"
$1 means "the thing we matched in parentheses"
| means "or"
^ means "start of string"
$ means "end of string"
. means "any character" so to mean "an actual dot" you have to type "\\." in a Swift string.
Notice that I check for both "..." and "." in the same regular expression. You kind of have to do something like that, or else the "." will match three times inside the "...". Another approach would be to first replace "..." with "…" (the single ellipsis character, typed on a Mac by pressing Opt-;). Then "…" is a one-character punctuation. (You could also decide to re-expand all ellipsis back to dot-dot-dot at the end of the process.)
Something like this is probably how I'd do it in real life, get it done and shipped, but it may be worth the pain/practice to try to build this as a character-by-character state machine, walking one character at a time, and keeping track of your current state.
You can try something like
string.replacingOccurrences(of: " ,", with: ",") for every punctuation...
Interesting problem; here's my stab at a non-Regex approach:
func correct(input: String) -> String {
typealias Correction = (punctuation: String, replacement: String)
let corrections: [Correction] = [
(punctuation: "...", replacement: "... "),
(punctuation: "'", replacement: "'"),
(punctuation: ",", replacement: ", "),
]
var transformed = input
for correction in corrections {
transformed = transformed
.components(separatedBy: correction.punctuation)
.map({ $0.trimmingCharacters(in: .whitespaces) })
.joined(separator: correction.replacement)
}
return transformed
}
let testInput = "Hello , World ... I 'm a newbie iOS Developer."
let testOutput = correct(input: testInput)
// Hello, World... I'm a newbie iOS Developer.
If you were doing this manually by processing characters arrays, you would merely need to check the previous and next characters around spaces. You can achieve the same result using functional style programming with zip, filter and map:
let testInput = "Hello , World ... I 'm a newbie iOS Developer."
let punctuation = Set(".\',")
let previousNext = zip( [" "] + testInput, String(testInput.dropFirst()) + [" "] )
let filteredChars = zip(Array(previousNext),testInput)
.filter{ $1 != " "
|| !($0.0 != " " && punctuation.contains($0.1))
}
let filteredInput = String(filteredChars.map{$1})
print(testInput) // Hello , World ... I 'm a newbie iOS Developer.
print(filteredInput) // Hello, World... I'm a newbie iOS Developer.
Swift 4, 4.2 and 5
let str = " Akbar Code "
let trimmedString = str.trimmingCharacters(in: .whitespaces)