-1

I want to extract the slug between (%) and (?) from a String url?

This is the url as string.

https://xyz.page.link/product/prdt%3D29c1118b344a53949824990eec6bd267?amv=24&apn=com.example.example&ibi=com.example.example&imv=1.0.1&isi=1613285148&link=https%3A%2F%2Fxyz.page.link%2F

I want to extract the string between % and ? from this part prdt%3D29c1118b344a53949824990eec6bd267?

Is that possible?

Thanks

Danny
  • 287
  • 7
  • 30
  • What have you tried? This should be pretty trivial with a regular expression or with [`String.indexOf`](https://api.dart.dev/stable/dart-core/String/indexOf.html) and [`String.substring`](https://api.dart.dev/stable/dart-core/String/substring.html). – jamesdlin Apr 16 '22 at 06:06
  • I have tried a lot, don't have experience with regex and still a beginner with coding. It would be great if you can help. – Danny Apr 16 '22 at 06:08
  • have you heard about the ``parse`` method in dart? – OMi Shah Apr 16 '22 at 06:08
  • 2
    "I want to extract the slug between (%) and (?) from a String url" No, you don't. You just don't know it yet. What do you want to do with "3D29c1118b344a53949824990eec6bd267", what is it supposed to match? Look up a thing called URL encoding. What you need is probably the "29c1118b344a53949824990eec6bd267" product id, although if this URL is formed in that way, it looks like the orignal server code author did not know what they were doing either. The mess has come full circle. Better to go back to the drawing board and go over the requirements again. Make sure they are correct before coding. – nvoigt Apr 16 '22 at 06:51

1 Answers1

2

Yes you can use RegExp class.

Please take a look at this answer: How to use RegEx in Dart?

void main() {
    var a ='https://xyz.page.link/product/prdt%3D29c1118b344a53949824990eec6bd267?amv=24&apn=com.example.example&ibi=com.example.example&imv=1.0.1&isi=1613285148&link=https%3A%2F%2Fxyz.page.link%2F';
    var regexp = RegExp(r"(?<=\%).+?(?=\?)");
    print(regexp.hasMatch(a)); // true
    print(regexp.firstMatch(a)?.group(0)); // 3D29c1118b344a53949824990eec6bd267
}
ethicnology
  • 474
  • 5
  • 13