-3

I am trying to find all strings that follows a specific pattern in a python string.

"\[\[Cats:.*\]\]"

However if many occurrences of such pattern exist together on a line in a string it sees the pattern as just one, instead of taking the patterns separately.

strng = '[[Cats: Text1]] said I am in the [[Cats: Text2]]fhg is abnorn'
x = re.findall("\[\[Cats:.*\]\]", strng) 

The output gotten is:

['[[Cats: Text1]] said I am in the [[Cats: Text2]]']

instead of

['[[Cats: Text1]]', '[[Cats: Text2]]']

which is a list of lists.

What regex do I use?

3 Answers3

1

"\[\[Cats:.*?\]\]"

Your current regex is greedy - it's gobbling up EVERYTHING, from the first open brace to the last close brace. Making it non-greedy should return all of your results.

Demo

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
1

The problom is that you are doing a greedy search, add a ? after .* to get a non greedy return.

code follows:

import re

strng = '[[Cats: Text1]] said I am in the [[Cats: Text2]]fhg is abnorn'
regex_template = re.compile(r'\[\[Cats:.*?\]\]') 
matches = re.findall(regex_template, strng)
print(matches)
-1

Don't do .*, because that will never terminate. .* means any character and not even one occurence is required.

import re

strng = '''[[Cats: lol, this is 100 % cringe]] 
said I am in the [[Cats: lol, this is 100 % cringe]] 
fhg is abnorn'''

x = re.findall(r"\[\[Cats: [^\]]+\]\]", strng) 
print(x)
J.Doe
  • 224
  • 1
  • 4
  • 19
  • Regex fails if the string inside the brackets contains a close bracket. Also misleading in that `.*` "terminates" just fine. – Nick Reed Oct 16 '19 at 20:13