0

I have a line like this, with a \t at the beginning:

    {ABC-1234-22 ABC-2345-4444}

With my first regex

\t\{(ABC-\d{4}-\d+)\s?\}

I find only one match. In another try I find one or two but not more:

\t\{(ABC-\d{4}-\d+)\s?(ABC-\d{4}-\d+)*\}

But I want to get 1 or n matches.

How can I achieve this?

JohnnyFromBF
  • 9,873
  • 10
  • 45
  • 59
  • 2
    You may use `\t\{ABC-\d{4}-\d+(?:\s+ABC-\d{4}-\d+)*\}`, see [demo](https://regex101.com/r/mHQu2S/3). There is no need capturing the groups since you can't access them later with Python `re`. If you need them, grab the match with the regex above and run `re.findall(r'ABC-\d{4}-\d+', match)` on the match(es). – Wiktor Stribiżew Mar 25 '19 at 14:17
  • As seen in the comment of @WiktorStribiżew, you are missing the whitespace which should be repeated also multiple times. Currently you are matching `CVE-1234-12 CVE1234-236CVE8697-4256CVE1245-12`. As you notice, no spaces. – kvantour Mar 25 '19 at 14:20
  • But I need each ABC-xxxx-xx as a group match. – JohnnyFromBF Mar 25 '19 at 14:20
  • @JohnnyFromBF, what do you mean, you want to reference it again later on? – kvantour Mar 25 '19 at 14:20
  • Then re-read my comment. Or are you using PyPi regex module? – Wiktor Stribiżew Mar 25 '19 at 14:20
  • You can get each match as a separate group by using `(?:\t\{|(?!^)\G)\s*(ABC-\d{4}-\d+)(?=[^{]*})` though it requires `regex` module instead of `re`: Check this working here: https://regex101.com/r/znTUCv/2 – anubhava Mar 25 '19 at 14:21
  • 1
    @anubhava With `regex`, OP would have access to all the captures and `r'\t\{(?:\s*(ABC-\d{4}-\d+))*\s*}'` [would work](https://rextester.com/UEBO43991) too. – Wiktor Stribiżew Mar 25 '19 at 14:24
  • @WiktorStribiżew Thanks for the hint, I tried it but match is None in Python 3. Tried this: `rw = re.compile(r'\t\{ABC-\d{4}-\d+(?:\s+ABC-\d{4}-\d+)*\}') match = rw.match(" {ABC-1111-22}") print(re.findall(r'ABC-\d{4}-\d+', str(match)))` – JohnnyFromBF Mar 25 '19 at 14:37
  • @JohnnyFromBF Mind the right package, [PyPi regex](https://pypi.org/project/regex/). And that is not a hint, that is a solution. The first comment shows the basic solution for the basic `re` library. – Wiktor Stribiżew Mar 25 '19 at 14:39
  • @WiktorStribiżew Well for me it doesn't work, but okay. – JohnnyFromBF Mar 25 '19 at 14:40
  • [It works well](https://rextester.com/MZGPAY93766). When it "doesn't work", you must always provide a demo link, otherwise, no one can help you fix the issue. – Wiktor Stribiżew Mar 25 '19 at 14:43
  • @WiktorStribiżew Okay, will do. Thanks! – JohnnyFromBF Mar 25 '19 at 14:45

0 Answers0