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?
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'