Home > Mobile >  Extract text using regular expression
Extract text using regular expression

Time:05-18

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

CodePudding user response:

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 \\

CodePudding user response:

This is a valid URL. The desired portion is the last path component

  • Swift

    let url = URL(string: "https://looney-tunes/review/case/CAAAAAAAAR-hw7QEAAAAMf___-A?caseGroup=12&queueId=52")!
    let caseID = url.lastPathComponent
    
  • Objective-C

    NSURL *url = [NSURL URLWithString: @"https://looney-tunes/review/case/CAAAAAAAAR-hw7QEAAAAMf___-A?caseGroup=12&queueId=52"];
    NSString *caseID = url.lastPathComponent;
    
  • Related