如何使用正则表达式在Python中查找模式时转义\triangle、\bold等

2024-04-28 20:34:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字符串,它是这样的:

\triangle \bold \new \regex

我已经使用了(re.findall(r"\\\w+",s),但它并没有给我结果,因为它显然不能识别\t, \b等我想要的方式。我怎样才能摆脱那些角色

我正在使用变量来存储字符串,因此我不能执行s = r'\triangle \bold'。我使用repr('\triangle \bold')作为解决方法,但它给了我'\\triangle','\\x08oldsymbol'

这些情况的出路是什么

\\triangle, \\bold\triangle, \bold这样的东西是我期待的


Tags: 方法字符串re角色new方式情况regex
3条回答
>>> txt = r"\triangle \bold \new \regex"   #Notice the leading r
>>> txt
'\\triangle \\bold \\new \\regex'
>>> txt.split('\\')
['', 'triangle ', 'bold ', 'new ', 'regex']

您已经提到,您正在使用变量s来存储字符串,而不是在其中使用r前缀。所以有一个问题。如果字符串中有\u\x\U\N,则将引发SyntaxError。 例如:

>>> s = 'There is no way o\ut'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 17-18: truncated \uXXXX escape
>>> s = 'Cross symbol(\x) says it is wrong'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 13-14: truncated \xXX escape
>>> s = 'What an escape Seque\Nce'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 20-21: malformed \N character escape
>>> s = 'What an escape Seq\Uence'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 18-20: truncated \UXXXXXXXX escape

因此,如果我假设您的字符串没有\u\x\U\N,那么您可以尝试以下方法:

>>> import re
>>> def repEsc(s):
        s = re.sub('\a', r'\\a', s)
        s = re.sub('\b', r'\\b', s)
        s = re.sub('\f', r'\\f', s)
        s = re.sub('\v', r'\\v', s)
        s = re.sub('\n', r'\\n', s)
        s = re.sub('\r', r'\\r', s)
        s = re.sub('\t', r'\\t', s)
        return s
>>> s = '\triangle \bold \new \regex'
>>> s = repEsc(s)
>>> s
'\\triangle \\bold \\new \\regex'
>>> print(s)
\triangle \bold \new \regex

不确定这是否只是一个解决方法,但您可以从头开始重建字符串。试试这个:

import re

string = "\triangle \bold \new \regex"


escape_dict = {
    '\a' : r'\a',
    '\b' : r'\b',
    '\c' : r'\c',
    '\f' : r'\f',
    '\n' : r'\n',
    '\r' : r'\r',
    '\t' : r'\t',
    '\v' : r'\v',
    '\'' : r'\'',
    '\"' : r'\"'
}

def raw(string):
    new_string = ""
    for char in string:
        try: 
            new_string += escape_dict[char]
        except KeyError: 
            new_string += char
    return new_string

matches = re.findall(r"\\\w+", raw(string))
print(matches)

但是,我想看看你是否可以在代码的前面修改一些东西

相关问题 更多 >