D
D
DollyPapper2017-10-22 19:32:11
Python
DollyPapper, 2017-10-22 19:32:11

Is it possible to make 2 conditions in a pattern?

Is it possible to make the pattern satisfy two conditions at once in one regex pattern?
For example, if there is a line 'aaacd aaa bbbcd cccdd', is it possible to break this line there so that it cuts off the endings of words that end in 'cd|dd' for example, and at the same time break the same line on spaces? Or do you need to first write a regular expression that breaks the endings, and then feed it into another regular expression that will break it by spaces?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2017-10-22
@DollyPapper

import re

s = "aaacd aaa bbbcd cccdd"
print(re.findall(r'\b(\w+)(?:cd|dd)\b|\b(\w+)\b', s))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question