Sobes.tech
Junior — Middle

Can you explain what a regular expression is and how it is used for searching and processing text?

sobes.tech AI

Answer from AI

Regular expression (regex) is a pattern for searching and processing text. It allows you to describe complex search rules, for example, find all words starting with a certain letter, or validate email format.

In iOS, the NSRegularExpression class is used for working with regular expressions. It helps to find matches, replace text, and validate strings.

Example of finding all numbers in a string in Swift:

let text = "Price: 100, discount: 20%"
let pattern = "\\d+"
let regex = try! NSRegularExpression(pattern: pattern)
let matches = regex.matches(in: text, range: NSRange(text.startIndex..., in: text))
for match in matches {
    if let range = Range(match.range, in: text) {
        print(text[range])  // Will output "100" and "20"
    }
}
Can you explain what a regular expression is and how… - sobes.tech