开发者

Write a regex for a pattern?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜