我可以在 Python RegEx 中混合字符类吗? [英] Can I mix character classes in Python RegEx?

查看:50
本文介绍了我可以在 Python RegEx 中混合字符类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python RegEx 中的特殊序列(字符类)是像 \w\d 这样匹配一组字符的转义符.

Special sequences (character classes) in Python RegEx are escapes like \w or \d that matches a set of characters.

就我而言,我需要能够匹配除数字之外的所有字母数字字符.

In my case, I need to be able to match all alpha-numerical characters except numbers.

\w减去\d.

我需要使用特殊序列 \w 因为我正在处理 非 ASCII 字符并且需要匹配诸如Æ"和Ø"之类的符号.

I need to use the special sequence \w because I'm dealing with non-ASCII characters and need to match symbols like "Æ" and "Ø".

有人认为我可以使用这个表达式:[\w^\d] 但它似乎不匹配任何东西,我不知道为什么.

One would think I could use this expression: [\w^\d] but it doesn't seem to match anything and I'm not sure why.

简而言之,如何在 Python 正则表达式中混合(加/减)特殊序列?

So in short, how can I mix (add/subtract) special sequences in Python Regular Expressions?

EDIT:我不小心使用了 [\W^\d] 而不是 [\w^\d].后者确实匹配某些内容,包括括号和逗号,就我而言,它们不是字母数字字符.

EDIT: I accidentally used [\W^\d] instead of [\w^\d]. The latter does indeed match something, including parentheses and commas which are not alpha-numerical characters as far as I'm concerned.

推荐答案

你可以使用r"[^\W\d]",即.反转非字母数字和数字的并集.

You can use r"[^\W\d]", ie. invert the union of non-alphanumerics and numbers.

这篇关于我可以在 Python RegEx 中混合字符类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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