Ruby 正则表达式是否有像“!~"这样的不匹配运算符?在 Perl 中? [英] Does Ruby regular expression have a not match operator like "!~" in Perl?

查看:47
本文介绍了Ruby 正则表达式是否有像“!~"这样的不匹配运算符?在 Perl 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道 ruby​​ regex 是否有不匹配运算符,就像 perl 中的 !~ 一样.我觉得使用 (?!xxx)(?<!xxxx) 很不方便,因为你不能在 xxx 部分使用正则表达式.

I just want to know whether ruby regex has a not match operator just like !~ in perl. I feel it's inconvenient to use (?!xxx)or (?<!xxxx) because you cannot use regex patterns in the xxx part.

推荐答案

Yes: !~ 工作得很好——你可能认为不会,因为 Regexp 的文档页面中缺少它.尽管如此,它还是有效的:

Yes: !~ works just fine – you probably thought it wouldn’t because it’s missing from the documentation page of Regexp. Nevertheless, it works:

irb(main):001:0> 'x' !~ /x/
=> false
irb(main):002:0> 'x' !~ /y/
=> true

这篇关于Ruby 正则表达式是否有像“!~"这样的不匹配运算符?在 Perl 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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