Hi! The documentation at Apple Developer Documentation
mentions that the flag values (options) may be specified within the pattern using the (?ismx-ismx) pattern options.
I could not find an example of how to write these options in the pattern instead of using the options parameter.
How does one write the flags into the pattern?
Hi @nunop, you specify the ismwx options within parenthesis, like this:
"Hello (?i)world"
In this instance, even if your regular expression didn’t have the NSRegularExpression.Options.caseInsensitive option set, “world” would be matched with any capitalisation.
Here’s some example code you can copy into a Playground:
import Foundation
// Here, we have a regular expression which is case sensitive. Consequently we match only the final, all lowercase hello and the final, all lowercase world, for 2 total matches
if let regex = try? NSRegularExpression(pattern: "hello|world", options: []) {
let haystack = "Hello HELLO hello World WORLD world"
let range = NSMakeRange(0, haystack.count)
let matches = regex.matches(in: haystack, options: [], range: range)
print("Found \(matches.count) match(es)")
} else {
print("Invalid pattern")
}
// Here, we have the same pattern, but we've made the entire thing case insensitive, so we match all three hellos and all three worlds, for 6 total matches.
if let regex = try? NSRegularExpression(pattern: "hello|world", options: [.caseInsensitive]) {
let haystack = "Hello HELLO hello World WORLD world"
let range = NSMakeRange(0, haystack.count)
let matches = regex.matches(in: haystack, options: [], range: range)
print("Found \(matches.count) match(es)")
} else {
print("Invalid pattern")
}
// Finally, we have the same pattern but using the ?ismwx flags, which apply to only a part of a pattern. So in this case, we set the second part of the pattern to be case insensitive matching world with any case, but keep hello case sensitive, for 4 total matches
if let regex = try? NSRegularExpression(pattern: "hello|(?i)world", options: []) {
let haystack = "Hello HELLO hello World WORLD world"
let range = NSMakeRange(0, haystack.count)
let matches = regex.matches(in: haystack, options: [], range: range)
print("Found \(matches.count) match(es)")
} else {
print("Invalid pattern")
}
I’ve just implemented the first part of the tutorial where I can search for text and see any matches highlighted in yellow. I searched for eee and expected all the e’s in eeeeeeeevil to be highlighted but only the first six are, i.e. eeeeeeeevil. I don’t understand why the last highlighted e along with the two unhighlighted e’s aren’t a match as well. All the e’s are highlighted if I search for ee or eeee.
Hi @magnas. This is as expected. When the regular expression matches a range in the string the Regex engine moves on to the following character after the end of the match and not the next character after the beginning of the match.
You can see this clearly using the following regex testing site (which isn’t Swift specific) , which highlights each matched range independently (or by breakpointing in Xcode and inspecting the range for each match. I added breakpoints at lines 109 and 111 in SearchViewController)
Escaping square brackets is the same as escaping other reserved characters, you just need a two slashes in your pattern, like this:
"\\["
If that’s not working for you then you likely have a different underlying issue. As @shogunkaramazov mentions if you can provide the exact code you’re using I’ll see what I can do!
oh, I bet this is because you’ve not escaped the dash properly. This means the regex engine will be treating =-~ as a character range between = (61 in ASCII) and ~ (126 in ASCII), which is basically everything you care about other than digits.
If I’ve understood correctly, use the following string as the pattern input for your regular expression and you should be all good:
As for the “[”, I tried in www.regexr.com, it works as is without any escape character. But when I inserted to my Swift program, it allows “[” as well as other unwanted characters. So I used “\\[” as you suggested.
As for the “=-~”, our code is actually “=\\-~”, which allows the minus sign in our set.
So, it is good like this, as I don’t need the double quote.