0
votes

I'm trying to parse linux version with the following regex

^Linux version ([0-9a-z.-]*) \([^@]*@[^)]*).*

This is the result

Traceback (most recent call last):
File "", line 1, in module
File "/usr/lib/python2.6/re.py", line 190, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.6/re.py", line 245, in _compile
raise error, v # invalid expression
sre_constants.error: unbalanced parenthesis

How to resolve this?, is there something wrong with my regex?. Thanks

1

1 Answers

1
votes
^Linux version ([0-9a-z\.\-]*) ([^@]*@[^)]*).*

You didn't escape the dash in the first set of square brackets. A dash has a special value (it denotes a set of values, aka 0 to 9, A to Z. Therefore it must be escaped.

You also escaped a circle bracket "(" which I assume you did not mean to, as that results in unbalanced brackets. If you did want to, escape the second bracket as well (see below).

Alternative:

^Linux version ([0-9a-z\.\-]*) \([^@]*@[^)]*\).*