restrict 1 word as case sensitive and other as case insensitive in python regex | (pipe)

后端 未结 2 1893
猫巷女王i
猫巷女王i 2020-12-01 22:21

I got the meaning of | (pipe special character) in regex, Python. It matches either 1st or 2nd.

ex : a|b Matches either a or b.

<

2条回答
  •  Happy的楠姐
    2020-12-01 22:49

    You could generate the lower/upper case regex for the second word, and keep casing active:

    my_regex = "PuNe|"+"".join("[{}{}]".format(x.upper(),x.lower()) for x in "MaHaRaShTrA")
    

    that generates: PuNe|[Mm][Aa][Hh][Aa][Rr][Aa][Ss][Hh][Tt][Rr][Aa]

    and re.search(my_regex,s1) without any option does what you want.

提交回复
热议问题