Regex experts please help! I have the following two examples:
'(JEN) This is a sentence.'
'This is another sentence (412).'
I am trying to extract the different possible elements of these two sentences in the following way (knowing that there are three possible element types):
['JEN', 'This is a sentence', None]
[None, 'This is another sentence', 412]
Does anybody know how to solve this?
I tried the following regex:
r'(\(([A-Z]{3})\))?\s*([\w- ]+)?\s*(\(([0-9]{3})\))?'
r'(?:\(([A-Z]{3})\)\s*)(?:([\w- ]+))(?:\(([0-9]{3})\))' # Passive Groups
And for both I get errors for Invalid regular expressions.
Any ideas why?
sre_constants.error: bad character rangeoccurs because[\w- ]is interpreted as a range. It’s possible to use[\w -], but generally-should be escaped inside character classes:[\w\- ].Also, your expressions are not equivalent (aside from grouping). I’m not sure whether that was intentional, but note that the non-capturing version of
(regex)?is(?:regex)?, not(?:regex). In order to behave akin to the first expression, the second one should be: