0

I got a string like this ...

var string = "A12345678B119292A88B2222A883849B123"

---> (var string = "A12345678B119292A88B2222A883849B123")

... and like to split the string into specific groups starting with 'A' and ending with 'B' to get as result something like this:

resultArray: "12345678", "88", "883849"

This is what I found so far:

func matches(for regex: String, in text: String) -> [String] {

    do {
        let regex = try NSRegularExpression(pattern: regex)
        let results = regex.matches(in: text,
                                    range: NSRange(text.startIndex..., in: text))
        return results.map {
            String(text[Range($0.range, in: text)!])
        }
    } catch let error {
        print("invalid regex: \(error.localizedDescription)")
        return []
    }
}

let string = "A12345678B119292A88B2222A883849B123"
let pattern = "\\.*A.*B" // pattern error

let array = matches(for: pattern, in: string)
print(array)

[?] How is it possible to achieve this result using regex and Swift?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Also, please consider accepting/upvoting (green tick on the left) an answer [**to your other question**](https://stackoverflow.com/questions/48137343/how-to-use-regex-to-split-string-into-groups-of-identical-characters) if it helped you. – Jan Jan 07 '18 at 14:46

2 Answers2

1

You can achieve that with "positive lookbehind/lookahead" (compare Lookahead and Lookbehind Zero-Length Assertions):

let string = "A12345678B119292A88B2222A883849B123"
let pattern = "(?<=A).*?(?=B)"

let array = matches(for: pattern, in: string)
print(array)
// ["12345678", "88", "883849"]

The pattern matches an arbitrary sequence of characters, preceded by an "A" and followed by a "B".

Note also the use of the "non-greedy" pattern .*?, otherwise you would get a single match between the first "A" and the last "B".

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • The question is vaguely phrased, admittedly. I just inferred the need for digits from the `resultArray` but I might as well be wrong here. – Jan Jan 07 '18 at 14:57
0

Unless you have other examples, you may use

(?<=A)\d+(?=B)

See a demo on regex101.com.


Note that you need to double escape backslashes, so that you need to write (?<=A)\\d+(?=B) instead.
Jan
  • 42,290
  • 8
  • 54
  • 79
  • `let pattern = "[AB]\d+"` will lead to the error `"Invalid escape sequence in literal"` –  Jan 07 '18 at 14:38
  • This would match any sequence of digits starting with A or B. OP wants only to match digits starting with A and ending with B. So the result would be `["12345678", "119292", "88", "2222", "883849", "123"]` and thats not what OP asked – Leo Dabus Jan 07 '18 at 14:52
  • @LeoDabus: You are absolutely right, have updated the answer. – Jan Jan 07 '18 at 14:55