异或正则表达式 [英] Exclusive Or in Regular Expression

查看:68
本文介绍了异或正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找一些正则表达式帮助.我想设计一个表达式,匹配带有foo"或bar"的字符串,但不能同时匹配foo"和""

Looking for a bit of regex help. I'd like to design an expression that matches a string with "foo" OR "bar", but not both "foo" AND "bar"

如果我做类似...

/((foo)|(bar))/

它将匹配foobar".不是我要找的.那么,如何仅在存在一个术语或另一个术语时才进行正则表达式匹配?

It'll match "foobar". Not what I'm looking for. So, how can I make regex match only when one term or the other is present?

谢谢!

推荐答案

你可以用一个正则表达式来做到这一点,但我建议为了可读性你做一些像......

You can do this with a single regex but I suggest for the sake of readability you do something like...

(/foo/ and not /bar/) || (/bar/ and not /foo/)

这篇关于异或正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆