21

I am having some trouble wrapping my head around Python regular expressions to come up with a regular expression to extract specific values.

The page I am trying to parse has a number of productIds which appear in the following format

\"productId\":\"111111\"

I need to extract all the values, 111111 in this case.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
greyfox
  • 6,426
  • 23
  • 68
  • 114

4 Answers4

35
t = "\"productId\":\"111111\""
m = re.match("\W*productId[^:]*:\D*(\d+)", t)
if m:
    print m.group(1)

meaning match non-word characters (\W*), then productId followed by non-column characters ([^:]*) and a :. Then match non-digits (\D*) and match and capture following digits ((\d+)).

Output

111111
perreal
  • 94,503
  • 21
  • 155
  • 181
15

something like this:

In [13]: s=r'\"productId\":\"111111\"'

In [14]: print s
\"productId\":\"111111\"

In [15]: import re

In [16]: re.findall(r'\d+', s)
Out[16]: ['111111']
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
2

The backslashes here might add to the confusion, because they are used as an escape character both by (non-raw) Python strings and by the regexp syntax.

This extracts the product ids from the format you posted:

re_prodId = re.compile(r'\\"productId\\":\\"([^"]+)\\"')

The raw string r'...' does away with one level of backslash escaping; the use of a single quote as the string delimiter does away with the need to escape double quotes; and finally the backslashe are doubled (only once) because of their special meaning in the regexp language.

You can use the regexp object's findall() method to find all matches in some text:

re_prodId.findall(text_to_search)

This will return a list of all product ids.

Tobia
  • 17,856
  • 6
  • 74
  • 93
0

Try this,

 :\\"(\d*)\\"

Give more examples of your data if this doesn't do what you want.

frickskit
  • 624
  • 1
  • 8
  • 19