-3

I have a string I am trying to create a regex for in order to extract everything inside the brackets. An example of such a string is as follows

[-At(A),+CarAt(B),-CarAt(A),-InCar]

The current regex I'm using is re.search(r'\[.*?\]', string), but this only returns -At(A),-InCar instead of -At(A),+CarAt(B),-CarAt(A),-InCar

I am not sure why it's matching one set of parentheses in -At(A); I thought the regex I had would work because it would match everything between the brackets.

How can I get everything inside the brackets of my original string?

ti7
  • 16,375
  • 6
  • 40
  • 68
greendaysbomb
  • 364
  • 2
  • 6
  • 23
  • 1
    Edit into your question some minimal exact code you're running to get this result. Include import, the variable initialisation with the string you're going to search in, and printing the result, and show the result. – DisappointedByUnaccountableMod Oct 27 '20 at 14:25

2 Answers2

1

I think the problem is with the question mark. Because question marks, when they come after a quantifiers make them 'lazy'. So try to use:

r'\[.*\]'
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

You didn't say you wanted the contained members, but I suspect it to be the eventual case

To do so, I've found it better to slice or .strip() brackets off and then .split() this sort of string to get its members before doing further validation

>>> s = "[-At(A),+CarAt(B),-CarAt(A),-InCar]"
>>> s = s.strip('[]')
>>> s
'-At(A),+CarAt(B),-CarAt(A),-InCar'
>>> values = s.split(',')
>>> values
['-At(A)', '+CarAt(B)', '-CarAt(A)', '-InCar']

Using a regex to validate the individual results of this is often

  • easier to write and explain
  • is better at highlighting mismatches than re.findall(), which will silently omit mismatches
  • can be much more computationally efficient (though it may not be for your case) than trying to do the operation in a single step (ex1 ex2)
>>> import re
>>> RE_wanted = re.compile(r"[+-](At|Car|In){1,2}(\([A-Z]\))?")
>>> all((RE_wanted.match(a) for a in values))
True
ti7
  • 16,375
  • 6
  • 40
  • 68