如何在Python中为正则表达式的一部分设置忽略大小写标志?

发布于 2024-08-06 04:47:54 字数 240 浏览 2 评论 0原文

是否有可能在Python中实现这样一个简单的东西:

#!/usr/bin/perl
my $a = 'Use HELLO1 code';
if($a =~ /(?i:use)\s+([A-Z0-9]+)\s+(?i:code)/){
    print "$1\n";
}

字符串中间的标记字母始终是大写的。其余单词的字母可以有任何大小写(USE、use、Use、CODE、code、Code 等)

Is it possible to implement in Python something like this simple one:

#!/usr/bin/perl
my $a = 'Use HELLO1 code';
if($a =~ /(?i:use)\s+([A-Z0-9]+)\s+(?i:code)/){
    print "$1\n";
}

Letters of token in the middle of string are always capital. Letters of the rest of words can have any case (USE, use, Use, CODE, code, Code and so on)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如荒岛 2024-08-13 04:47:54

从 python 3.6 开始,您可以在组内使用标志:

(?imsx-imsx:...)

(“i”、“m”、“s”、“x”集合中的零个或多个字母,可选地后跟“-”,后跟同一集合中的一个或多个字母。)设置或删除的字母相应的标志:re.I(忽略大小写)、re.M(多行)、re.S(点匹配所有)和 re.X(详细),用于表达式的部分。

因此 (?i:use) 现在是正确的语法。从 python3.6 终端:

>>> import re
>>> regex = re.compile('(?i:use)\s+([A-Z0-9]+)\s+(?i:code)')
>>> regex.match('Use HELLO1 code')
<_sre.SRE_Match object; span=(0, 15), match='Use HELLO1 code'>
>>> regex.match('use HELLO1 Code')
<_sre.SRE_Match object; span=(0, 15), match='use HELLO1 Code'>

Since python 3.6 you can use flag inside groups :

(?imsx-imsx:...)

(Zero or more letters from the set 'i', 'm', 's', 'x', optionally followed by '-' followed by one or more letters from the same set.) The letters set or removes the corresponding flags: re.I (ignore case), re.M (multi-line), re.S (dot matches all), and re.X (verbose), for the part of the expression.

Thus (?i:use) is now a correct syntaxe. From a python3.6 terminal:

>>> import re
>>> regex = re.compile('(?i:use)\s+([A-Z0-9]+)\s+(?i:code)')
>>> regex.match('Use HELLO1 code')
<_sre.SRE_Match object; span=(0, 15), match='Use HELLO1 code'>
>>> regex.match('use HELLO1 Code')
<_sre.SRE_Match object; span=(0, 15), match='use HELLO1 Code'>
枯寂 2024-08-13 04:47:54

据我所知,python 正则表达式引擎不支持部分忽略大小写。这是一个使用不区分大小写的正则表达式的解决方案,然后测试令牌是否为大写。

#! /usr/bin/env python

import re

token_re = re.compile(r'use\s+([a-z0-9]+)\s+code', re.IGNORECASE)
def find_token(s):
    m = token_re.search(s)
    if m is not None:
        token = m.group(1)
        if token.isupper():
            return token

if __name__ == '__main__':
    for s in ['Use HELLO1 code',
              'USE hello1 CODE',
              'this does not match',
             ]:
        print s, '->',
        print find_token(s)

这是程序的输出:

Use HELLO1 code -> HELLO1
USE hello1 CODE -> None
this does not match -> None

As far as I could find, the python regular expression engine does not support partial ignore-case. Here is a solution using a case-insensitive regular expression, which then tests if the token is uppercase afterward.

#! /usr/bin/env python

import re

token_re = re.compile(r'use\s+([a-z0-9]+)\s+code', re.IGNORECASE)
def find_token(s):
    m = token_re.search(s)
    if m is not None:
        token = m.group(1)
        if token.isupper():
            return token

if __name__ == '__main__':
    for s in ['Use HELLO1 code',
              'USE hello1 CODE',
              'this does not match',
             ]:
        print s, '->',
        print find_token(s)

Here is the program's output:

Use HELLO1 code -> HELLO1
USE hello1 CODE -> None
this does not match -> None
旧城烟雨 2024-08-13 04:47:54

根据文档,这是不可能的。 (?x) 语法仅允许您修改整个表达式的标志。因此,您必须将其拆分为三个正则表达式,并在另一个正则表达式之后应用它们手动执行“忽略大小写”:/[uU][sS][eE]...

According to the docs, this is not possible. The (?x) syntax only allows you to modify a flag for the whole expression. Therefore, you must split this into three regexp and apply them one after the other or do the "ignore case" manually: /[uU][sS][eE]...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文