Silviu St Silviu St - 1 month ago 9
Swift Question

Regex to extract string between a string and the third occurence of another string in swift?

I have a large HTML that I must parse it in swift and extract some id's.

How can I extract from :

data-max-position="NAME-661174248527044608-661303076113014786-BD1UO2FFu9QAAAAAAAAET
the following string
NAME-661174248527044608-661303076113014786
(number between ar not fixed size)?
I need to get data bwtween data-max-position=" and the third occurrence of "-"


What regex should I use to give me the information I need?

Until now for another kind of string I have used the following but I don't know what regex should I use for getting what is mentionated in the first part

let query = mystr
let regex = try! NSRegularExpression(pattern:"data-name-id=\"(.*?)\"", options: [])
let tmp = query as NSString
var results = [String]()

regex.enumerateMatches(in: query, options: [], range: NSMakeRange(0, query.characters.count)) { result, flags, stop in
if let range = result?.rangeAt(1) {
results.append(tmp.substring(with: range))
}
}

print(results) // ["test", "test1"]


that extracts me the stuff inside "data-name-id" and the second "

Answer

If you have a plain text string in the format you mention, you may use a regex to extract the portion of it:

pattern:"\\sdata-max-position=\"([^\"-]+-[^\"-]+-[^\"-]+)"

Details:

  • \\s - a whitespace
  • data-max-position=\" - literal data-max-position=" substring
  • ([^\"-]+-[^\"-]+-[^\"-]+) - Group 1 (your required value) matching
    • [^\"-]+ - one or more characters other than " and -
    • - - a hyphen
    • [^\"-]+-[^\"-]+ - see above.

Note that [^...] is a *negated character class that matches any characters that are not defined in the ranges/sets inside the character class.