Home > Net >  I want to extract data using regular expression in python
I want to extract data using regular expression in python

Time:11-27

I have a string = "ProductId=967164&Colour=bright-royal" and i want to extract data using regex so output will be 967164bright-royal.

I have tried with this (?:ProductId=|Colour=)(.*) in python with regex, but getting output as 967164&Colour=bright-royal.

Can anyone please help me to find out regex for it.

CodePudding user response:

You don't need a regex here, use urllib.parse module:

from urllib.parse import parse_qs, unquote

qs = "ProductId=967164&Colour=bright-royal"

d = parse_qs(unquote(qs))
print(d)

# Output:
{'ProductId': ['967164'], 'Colour': ['bright-royal']}

Final output:

>>> ''.join(i[0] for i in d.values())
'967164bright-royal'

Update

>>> ''.join(re.findall(r'=(\S*?)(?=&|$)', qs))
'967164bright-royal'

CodePudding user response:

The alternative matches on the first part, you can not get a single match for 2 separate parts in the string.

If you want to capture both values using a regex in a capture group:

(?:ProductId|Colour)=(\S*?)(?=%26|$)

Regex demo

import re

pattern = r"(?:ProductId|Colour)=(\S*?)(?=&|$)"
s = "ProductId=967164&Colour=bright-royal"
print(''.join(re.findall(pattern, s)))

Output

967164bright-royal

CodePudding user response:

If you must use a regular expression and you can guarantee that the string will always be formatted the way you expect, you could try this.

import re

pattern = r"ProductId=(\d )&Colour=(.*)"
string = "ProductId=967164&Colour=bright-royal"
matches = re.match(pattern, string)

print(f"{matches[1]}{matches[2]}")
  • Related