Skip to content Skip to sidebar Skip to footer

Alternative Regex To Match All Text In Between First Two Dashes

I'm trying to use the following regex \-(.*?)-|\-(.*?)* it seems to work fine on regexr but python says there's nothing to repeat? I'm trying to match all text in between the firs

Solution 1:

You can use re.search with this pattern:

-([^-]*)

Note that - doesn't need to be escaped.

An other way consists to only search the positions of the two first dashes, and to extract the substring between these positions. Or you can use split:

>>> 'aaaaa-bbbbbb-ccccc-ddddd'.split('-')[1]
'bbbbbb'

Post a Comment for "Alternative Regex To Match All Text In Between First Two Dashes"