忽略整个字符串模式的大小写 [英] Ignoring case for a whole pattern of strings

查看:52
本文介绍了忽略整个字符串模式的大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

regexp 不是我的强项,但我有一个我想要检查的 4 个字符串的列表.我创建了一个(对我来说)可读的模式,因此可以轻松添加更多内容,但我 99% 肯定不会.

regexp are not my strong point but I have a list of 4 strings that I want to check for. I created a pattern which is readable(to me) so more can be added easily but I am 99% sure none will be.

列表是,无论使用什么大小写,我都想匹配字符串.

The list is, and I want to match the string regardless of what case is used.

  • packstation - ( packstation | Packstation | PACKSTATION )
  • 包装站 - ( 包装站 | 包装站 | 包装站 | 包装站 | 包装站 )
  • paketstation - ( paketstation | Paketstation | PAKETSTATION )
  • paket station - ( paket station | Paket station| paket Station| Paket Station | PAKET STATION )

我使用的模式是

^(?!packstation|pack station|paketstation|paket station$).*$

我的问题是我无法忽略大小写.我曾尝试在多个地方添加 i 标志,但它只是破坏了模式识别,没有任何通过.

My problem is I am unable to ignore case. I have tried adding the i flag in multiple places but it just breaks the pattern recognition and nothing gets through.

不确定我是否需要创建一个更复杂的模式,或者 Demandware 的正则表达式是否有问题,它只接受一个模式.

Not sure if I need to create a more complex pattern or if its a problem with Demandware's regexp which just accepts a pattern.

推荐答案

您没有指明语言.如果您使用 Java、Python、Ruby、Perl、.NET 或 PCRE (PHP),您可以在您的模式中添加一个内部修饰符 (?i).在你的情况下:

You didn't indicate the language. If you are using Java, Python, Ruby, Perl, .NET, or PCRE (PHP), you can tack on an internal modifier (?i) to your pattern. In your case:

(?i)^(?!packstation|pack station|paketstation|paket station$).*$

你也可以将其缩短为:

(?i)^(?!pack ?station|paket ?station$).*$

对于这种情况,正则表达式在上面列出的所有语言中都具有相同的效果.

For this case, the regex will have the same effect in all the languages listed above.

注意正则表达式会拒绝:

Note that the regex will reject:

packstation
PackStation
PACKSTATION
pAcKstAtioN

所有大写/小写字母组合都将被拒绝.

All combination of uppercase/lowercase letters will be rejected.

在 Python 中,内部修饰符是全局的,您只能为整个模式打开它(无论您声明它的位置如何).

In Python, internal modifier is global, and you can only turn it on for the whole pattern (regardless of the position where you declare it).

在 Java、Ruby、Perl、.NET 或 PCRE 中,您可以通过在非捕获组 (?modifiers:pattern) 中使用内部修饰符来确定其范围.您也可以使用 (?modifiers) 从指定位置打开它,并使用 (?-modifiers) 从指定位置关闭它,因为在这些语言中,修饰符的作用范围限定在声明它的位置.

In Java, Ruby, Perl, .NET or PCRE, you can scope the internal modifier by using it in a non-capturing group (?modifiers:pattern). You can also turn it on from the specified position with (?modifiers) and turn it off from the specified position with (?-modifiers), since in these languages, the effect of the modifiers are scoped to the position where it is declared.

通常,大多数语言会给你一个选项来指定主模式之外的标志.它可能支持作为主模式的结束分隔符后的尾随字符,也可能在函数调用中支持.

Usually, most languages will give you an option to specify the flags outside the main pattern. It may support it as the trailing characters after the closing delimiter to the main pattern, or it may support it in the function call.

这篇关于忽略整个字符串模式的大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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