1

I am trying to regex match strings change://problem/59689726 and change://1234567but below code only matches the former,how do I change the regex to match both?how to make problem/ optional?

INPUT CODE:

import re
out = '''
<change://problem/59689726> This is a test1
change://1234567 This is a test2
[Problem]
This is problem desription
'''
m = re.findall("[\S]*(?:change:\/\/problem\/(\d{8,8}))", out.split('[Problem]')[0])
if m:
    for radar in set(m):
        print radar

CURRENT OUTPUT:-

59689726

EXPECTED OUTPUT:-

59689726 1234567

wisecrack
  • 315
  • 2
  • 12

1 Answers1

1

If you need to get any 1+ digits after change://problem/ or change:// you may use

re.findall("change://(?:problem/)?(\d+)", out.split('[Problem]')[0])

See the Python demo and the regex demo.

The pattern matches:

  • change:// - a literal string
  • (?:problem/)? - 1 or 0 occurrences of a problem/ substring
  • (\d+) - Group 1 (what will be returned by re.findall): 1+ digits
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • where can I understand/read about `(?:problem/)?` ?why we need `?:` – wisecrack Mar 27 '20 at 19:33
  • @user3682248 [Here](https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-in-regular-expressions) and [here](https://stackoverflow.com/a/31915134/3832970). – Wiktor Stribiżew Mar 27 '20 at 19:34