Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - How do I extract captured groups in regular expressions?

Tags:

I am using Swift 3 and trying to access captured groups.

let regexp = "((ALREADY PAID | NOT ALR | PROVIDER MAY | READY | MAY BILL | BILL YOU | PAID)((.|\\n)*))(( \\d+)(\\.+|-+)(\\d\\d))"  // check if some substring is in the recognized text if let range = stringText.range(of:regexp, options: .regularExpression) {     let result = tesseract.recognizedText.substring(with:range) } 

I want to be able to extract out the last two numbers captured (\d\d) so if the text was: ALREADY PAID asfasdfadsfasdf 39.15, it would extract 15. Here is a regex builder that shows what I want. Normally, I would be able to do $8 to get the 8th group that was extracted but I don't know how to do that in Swift 3.

http://regexr.com/3fh1e

like image 819
noblerare Avatar asked Mar 14 '17 15:03

noblerare


People also ask

What is first capturing group in regex?

First group matches abc. Escaped parentheses group the regex between them. They capture the text matched by the regex inside them into a numbered group that can be reused with a numbered backreference. They allow you to apply regex operators to the entire grouped regex.

How do Capturing groups work in regex?

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g" .

What is non capturing group in regex?

tl;dr non-capturing groups, as the name suggests are the parts of the regex that you do not want to be included in the match and ?: is a way to define a group as being non-capturing. Let's say you have an email address [email protected] . The following regex will create two groups, the id part and @example.com part.

What does capture mean in regex?

capturing in regexps means indicating that you're interested not only in matching (which is finding strings of characters that match your regular expression), but you're also interested in using specific parts of the matched string later on.


1 Answers

Swift 4, Swift 5

extension String {     func groups(for regexPattern: String) -> [[String]] {     do {         let text = self         let regex = try NSRegularExpression(pattern: regexPattern)         let matches = regex.matches(in: text,                                     range: NSRange(text.startIndex..., in: text))         return matches.map { match in             return (0..<match.numberOfRanges).map {                 let rangeBounds = match.range(at: $0)                 guard let range = Range(rangeBounds, in: text) else {                     return ""                 }                 return String(text[range])             }         }     } catch let error {         print("invalid regex: \(error.localizedDescription)")         return []     } } } 

example:

let res = "1my 2own 3string".groups(for:"(([0-9]+)[a-z]+) ") 

(lldb) po res ▿ 2 elements
▿ 0 : 3 elements

- 0 : "1my "  - 1 : "1my"  - 2 : "1"    

▿ 1 : 3 elements

- 0 : "2own "  - 1 : "2own"  - 2 : "2" 
like image 142
Vyacheslav Avatar answered Oct 25 '22 22:10

Vyacheslav