0

I need some help with Regex expression, as it s very new to me.

I have a URL which consists of Item Number or Product ID.

What I am looking to achieve is that could trim the URL part and extra part after a symbol of %.

Here is how the url looks like.

https://www.test.com/test-test/test/test-demo-demo-demo-demo.html?piid=12345678%2C24753325#seemoreoptions-b0uksl51j4m

OR

https://www.test.com/test-test/test/test-demo-demo-demo-demo.html?piid=12345678

So from the above URL I am looking to trim https://www.test.com/test-test/test/test-demo-demo-demo-demo.html?piid= and this part %2C24753325#seemoreoptions-b0uksl51j4m

So, this should give me only 12345678.

I have use the following Regex

(.*)(\=) Replace with $2

Above Regex does trim the url first part but does not the part after % symbol.

I tried to get solution on

https://regexr.com/

So for the both the above URL examples, I should get the result as

12345678

Thank you in advance

Riyaz Shaikh
  • 83
  • 11
  • Try replacing `.*=|%.*` with an empty string. – Wiktor Stribiżew Dec 23 '22 at 15:08
  • What programming/script-language do you need this in? Depending on the language/regex-flavor the solution may look different. E.g. if you have perl-like regex you could extract the number as match using `(?<=[?&]piid=)\d+` or `[?&]piid=\K\d+`. If you want to replace with the most basic regex, replace `^.*[?&]piid=` with an empty string and after that replace `[^0-9]*$` with an empty string. – Jay Dec 23 '22 at 16:17

1 Answers1

0

Instead of trimming part before and after digits you want, try another approach: extract digits you want.

You can use groups (parentheses) in regexp to extract found data.

piid=([0-9]+)

It means:

  • piid= - text to find
  • [0-9]+ - one or more digits
  • () - group

You can extract first group by $1 (or \1 etc. - depends of language you use).

Example: https://regexr.com/758d9

enter image description here

mkczyk
  • 2,460
  • 2
  • 25
  • 40