开发者

Write a regex for a pattern?

开发者 https://www.devze.com 2023-01-15 04:28 出处:网络
a + (ab or cd ) + g is my expression. How can I write a regex 开发者_如何学Cin Python to match these?To search this regex in a string, say

a + (ab or cd ) + g is my expression. How can I write a regex 开发者_如何学Cin Python to match these?


To search this regex in a string, say

re.search("a\\+((ab)|(cd))\\+g", your_string)

To extract matches, use re.findall etc. No need to copy&paste the docs here. :)

EDIT: Updated after OP changed the regex.

If you want it to match whitespace in between, things get pretty ugly ...

re.search("a\W*\\+\W*((ab)|(cd))\W*\\+\W*g", your_string)
0

精彩评论

暂无评论...
验证码 换一张
取 消