LarsVegas LarsVegas - 1 month ago 11
Python Question

Regex: skip the first match of a character in group?

From this string

s = 'stringalading-0.26.0-1'


I'd like to extract the part
0.26.0-1
. I can think of various ways to achieve this, using split or a regular expression using a pattern like this

pattern = r'\d+\.\d+\.\d+\-\d+'


I also tried to use a group of characters, like so:

pattern = r'[.\-\d]+'


This gives me:

In [30]: re.findall(pattern, s)
Out[30]: ['-0.26.0-1']


So I wondered: is it possible to skip the first occurrence of a character in a group, in this case the first occurrence of
-
?

Answer

is it possible to to skip the first occurrence of a character in a group, in this case the first occurrence of -?

NO, because when matching, the regex engine processes the string from left to right, and once the matching pattern is found, the matched chunk of text is written to the match buffer. Thus, either write a regex that only matches what you need, or post-process the found result by stripping unwanted characters from the left.

I think you do not need a regex here. You can split the string with - and pass the maxsplit argument set to 1, then just access the second item:

s = 'stringalading-0.26.0-1'
print(s.split("-", 1)[1])   # => '0.26.0-1'

See the Python demo

Also, your first regex works well:

import re
s = 'stringalading-0.26.0-1'
pat = r'\d+\.\d+\.\d+-\d+'
print(re.findall(pat, s)) # => ['0.26.0-1']