7

I want to find all data enclosed in [[ ]] these brackets.

[[aaaaa]] -> aaaaa

My python code (using re library) was

la = re.findall(r'\[\[(.*?)\]\]', fa.read())

What if I want to extract only 'a' from [[a|b]]

Any concise regular expression for this task? ( extract data before | )

Or should I use additional if statement?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
SUNDONG
  • 2,501
  • 5
  • 21
  • 37

1 Answers1

3

You can try:

r'\[\[([^\]|]*)(?=.*\]\])'

([^\]|]*) will match until a | or ] is found. And (?=.*\]\]) is a lookahead to ensure that ]] is matched on RHS of match.

Testing:

>>> re.search( r'\[\[([^\]|]*)(?=.*\]\])', '[[aaa|bbb]]' ).group(1)
'aaa'
>>> re.search( r'\[\[([^\]|]*)(?=.*\]\])', '[[aaabbb]]' ).group(1)
'aaabbb'
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Thank you for providing concise answer and explanation. I should read more articles about regular expression. – SUNDONG Sep 28 '15 at 03:30
  • Only one thing to note - this won't handle nested brackets (and in fact a regex wouldn't in general anyway without the help of a counter). – brandonscript Sep 28 '15 at 05:38