Python regex: Alternation for sets of words

前端 未结 6 1037
我在风中等你
我在风中等你 2020-12-19 17:41

We know \\ba\\b|\\bthe\\b will match either word \"a\" or \"the\"
I want to build a regex expression to match a pattern li

6条回答
  •  旧时难觅i
    2020-12-19 18:41

    As I understand you want some regex like this:

    (?:a|the|one)\s+(?:reason|reasons)\s+(?:for|of)
    

    It's so simple, just combine them by using groups.

    see: DEMO

    Note Your requirement above, its sound is not so strict for me, in case that you want to modify something by yourself, let's consider the explanation below

    Explanation

    (?:abc|ijk|xyz)

    Any word abc, ijk or xyz which grouped by non-capture group (?:...) means this word will not capture to regex variable $1, $2, $3, ....

    \s+

    This is word delimiter which here I set it as any spaces, + stands for 1 or more.

提交回复
热议问题