1

I am 2 days old to regex,

I have a small query

When I use the below regex statement

re.findall('([ad])[\-.\s+]([be])[\-.\s+]([cf])*', 'a-b-c d-e-f')

I recieve an output

[('a', 'b', 'c'), ('d', 'e', 'f')]

However the output I desire is

['a-b-c', 'd-e-f']

I've tried ?, $ and other expressions but no luck yet.

Please let me know If the expression can be molded a bit to get the desire output.

Thanks

3 Answers 3

2
print re.findall('((?:[ad])[\-.\s+](?:[be])[\-.\s+](?:[cf])*)', 'a-b-c d-e-f')

Capture the whole string and use non capturing groups for the rest.findall returns all the captured groups if present any.

Output : ['a-b-c', 'd-e-f']

Sign up to request clarification or add additional context in comments.

Comments

1

You have capturing groups around all your letters. If you use a single capturing group, then you will get what you want. Also, you should get rid of the trailing '*' because that matches the blank string:

>>> re.findall('([ad][\-.\s+][be][\-.\s+][cf])', 'a-b-c d-e-f')
['a-b-c', 'd-e-f']

Comments

1

What's wrong with string.split?

string.split()

or

re.findall(r'\S+', s)

Use re.finditer. This won't match a-b.c

>>> l = []
>>> for i in re.finditer(r'\b[ad]([-.\s+])[be]\1[cf]\b', 'a-b-c d-e-f'):
        l.append(i.group())


>>> l
['a-b-c', 'd-e-f']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.