正则表达式:否定字符 [英] Regex: Negating characters

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

问题描述

快速问题 -

你如何用 {x,y} 创建一种字符串语言,但用 (xy) 否定所有字符串?

How do you create a language of strings out of say {x,y}, but negating all strings with (xy)?

到目前为止我的尝试:

\bx*[^(xy)]*y\b OR \by*[^(xy)]*x\b OR \b[^(xy)][xy]*[^(xy)]*\b

最后一个限制最少,但由于 [^(xy)] 的多次使用似乎很笨拙.

The last of which is the least restricted, but seems clumsy with the multiple usage of [^(xy)].

完全否定包含 (xy) 但允许所有其他组合的字符串的最懒惰最方便的方法是什么?

What is the laziest most convenient method for completely negating a string which contains (xy), but allows all other combinations?

谢谢

允许的示例字符串:xxxxxxx yyyyyyyyy yxxxx yyyyyyxx

Editted: Example strings that are allowed: xxxxxxx yyyyyyyyy yxxxx yyyyyyxx

不允许的示例字符串:xxxxyxxx xyxxxx yyyyxyyy yyyxyxy 等

Example strings that are not allowed: xxxxyxxx xyxxxx yyyyxyyy yyyxyxy etc

推荐答案

如果我对挑战的理解正确,那么您所描述的字符串语言可以以任意数量的 y 开头,后跟任意数量的 x,如那些是唯一允许的两个字符,一旦出现 x 就不能放置 y ,因为这会导致字符串xy"出现.

If I understand the challenge correctly, you're describing a language of strings that can start with any number of y's, followed by any number of x's, as those are the only two characters allowed, and you can't place a y once an x has appeared because that would cause the string "xy" to appear.

\by*x*\b

当然,我假设您实际上是在为不像您提供的那样简单的情况寻找更通用的解决方案.在这种情况下,否定前瞻断言是最简单的解决方案.

Of course, I assume that you're actually looking for a more general solution for cases that aren't as simple as the one you give. In that case, a negative lookahead assertion is the easiest solution.

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

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