0

I am trying to use re.findall to capture two different outcomes off a webpage.

For example, I want to find both "save $10.00" and "$100.00" if the save exists and if it does not exist then just find the "$100".

I have tried save ?\$\d{1,5}.

Thanks in advance!

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397

1 Answers1

0

You may use

re.findall(r'(?:\bsave\s+)?\$\d+(?:\.\d+)?', s)

See the regex demo.

Details

  • (?:\bsave\s+)? - an optional non-capturing group matching
    • \bsave - whole word save
    • \s+ - 1+ whitespaces
  • \$ - a $ char
  • \d+ - 1+ digits
  • (?:\.\d+)? - an optional non-capturing group matching
    • \. - a dot
    • \d+ - 1+ digits

Mind that you need to use non-capturing groups to make whole sequences of pattern since re.findall only returns (list of) tuples if capturing groups are defined in the pattern and that causes lots of confusion.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397