user43405
user43405

Reputation: 1

search for substring allowing dashes python

Does anybody know how I can search for a substring allowing dashes between the characters?

Something like that.

AAABBC
AA---A--BB-C

I would like the above substring to match the one below.

Any response is apreciated.

Thank you.

Upvotes: 0

Views: 50

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

Add -* in-between those characters.

r'A-*A-*A-*B-*B-*C'

Example:

>>> re.match(r'^A-*A-*A-*B-*B-*C$', 'AA---A--BB-C')
<_sre.SRE_Match object; span=(0, 12), match='AA---A--BB-C'>
>>> re.match(r'^A-*A-*A-*B-*B-*C$', 'A----AA--BB-C')
<_sre.SRE_Match object; span=(0, 13), match='A----AA--BB-C'>
>>> re.match(r'^A-*A-*A-*B-*B-*C$', 'A--A-A--B-B-C')
<_sre.SRE_Match object; span=(0, 13), match='A--A-A--B-B-C'>

Upvotes: 1

Related Questions