Search code examples
iosregexmatchingnsregularexpression

Extract text using regular expression


I'm trying to extract a case id from the string, Could someone help me with this

https://looney-tunes/review/case/CAAAAAAAAR-hw7QEAAAAMf___-A?caseGroup=12&queueId=52

I want to extract the portion after case/ and before '?'in the link. They're 27 characters in length.

Thanks in advance


Solution

  • import re
    s='https://looney-tunes/review/case/CAAAAAAAAR-hw7QEAAAAMf___-A?caseGroup=12&queueId=52'
    x=re.search("(?s)(?<=case/).*?(?=\\?)",s)
    

    <re.Match object; span=(33, 60), match='CAAAAAAAAR-hw7QEAAAAMf___-A'>
    

    In general, If you want a substring between two string Use : (?s)(?<=str1).*?(?=str2)

    as this was our str2 -> ?, had to escape it using \\