Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python re.sub从字符串中删除单引号和双引号_Python_Regex_String_Re_Rawstring - Fatal编程技术网

Python re.sub从字符串中删除单引号和双引号

Python re.sub从字符串中删除单引号和双引号,python,regex,string,re,rawstring,Python,Regex,String,Re,Rawstring,有一个问题很快让我抓狂。我想从字符串中同时删除“和”字符。我想使用re.sub来执行此操作(因为我试图比较re.sub和str.replace,所以我想两种方式都执行此操作)。现在,我对原始字符串的理解是,除非转义字符转义打开字符串的字符,否则转义字符将被视为文本。因此,我有两种方法来实现此目的: # Method 1: concatenate strings that have different enclosing characters >>> REGEX1 = re.co

有一个问题很快让我抓狂。我想从字符串中同时删除“和”字符。我想使用re.sub来执行此操作(因为我试图比较re.sub和str.replace,所以我想两种方式都执行此操作)。现在,我对原始字符串的理解是,除非转义字符转义打开字符串的字符,否则转义字符将被视为文本。因此,我有两种方法来实现此目的:

# Method 1: concatenate strings that have different enclosing characters
>>> REGEX1 = re.compile(r"[" + r'"' + r"'" + r"]")
>>> REGEX1.pattern
'["\']'
# Method 2: Try to escape one of the quotation characters
>>> REGEX2= re.compile(r"[\"']")
>>> REGEX2.pattern
'[\\"\']'
给出的模式看起来不同。但它们是不是?我测试它们在正则表达式中的行为是否相同:

>>> test_string = "hello ' world \" "
>>> test_string
'hello \' world " '
>>> result_1 = REGEX1.sub(r'', test_string)
>>> result_2 = REGEX2.sub(r'', test_string)
>>> result_1
'hello  world  '
>>> result_2
'hello  world  '
>>> 
我的直觉告诉我,有两件事是可能的:

  • “[”]”等于“[\”]”
  • “[”]!=”[\“']”,但当作为正则表达式处理时,其行为将等效
  • 最后一个测试是:

    >>> '["\']' == '[\\"\']'                                                                                                                                                                                      
    False
    

    那么上面的2)是正确的语句吗?你能帮我理解发生了什么吗?

    当你显示它们的值时,它们看起来不同,但就解释为正则表达式而言,它们是等效的:

    import re
    
    
    REGEX1 = re.compile(r"[" + r'"' + r"'" + r"]")
    print(REGEX1.pattern)
    print(REGEX1.sub('', """abc"'def"""))
    REGEX2= re.compile(r"[\"']")
    print(REGEX2.pattern)
    print(REGEX2.sub('', """abc"'def"""))
    
    印刷品:

    ["']
    abcdef
    [\"']
    abcdef 
    
    pattern1 =  a
    b
    <re.Match object; span=(0, 3), match='a\nb'>
    pattern 2 = a\nb
    <re.Match object; span=(0, 3), match='a\nb'>
    
    解释

    原始字符串
    r'\n'
    和非原始字符串
    '\n'
    之间的差异是巨大的,因为后者是一个特殊的转义序列,等同于换行符,而前者相当于
    '\\n'
    ,即反斜杠的两个字符序列后跟字母n。但对于其他情况,例如正如
    '\“
    一样,如果反斜杠后跟双引号不是一个特殊的转义序列,那么反斜杠是多余的,可以忽略,因此
    [”]
    [\”]
    是等价的

    更新

    由于我指出了原始字符串中的转义序列与非原始字符串中的转义序列通常存在很大差异,当反斜杠后面的内容在反斜杠后面有特殊意义时(例如,
    r'\n'
    '\n'
    ),对于正则表达式的所有意图和目的来说,情况并非总是如此。例如,当在正则表达式中使用时,Python正则表达式引擎会将换行符与从两个字符序列
    r'\n'
    (即
    '\\n'
    )编译的正则表达式或换行符
    '\n'
    )匹配:

    import re
    
    
    REGEX1 = re.compile('a\nb') # use actual newline
    print('pattern1 = ', REGEX1.pattern)
    print(REGEX1.search('a\nb'))
    REGEX2 = re.compile(r'a\nb') # use '\\n'
    print('pattern 2 =', REGEX2.pattern)
    print(REGEX2.search('a\nb'))
    
    印刷品:

    ["']
    abcdef
    [\"']
    abcdef 
    
    pattern1 =  a
    b
    <re.Match object; span=(0, 3), match='a\nb'>
    pattern 2 = a\nb
    <re.Match object; span=(0, 3), match='a\nb'>
    
    pattern1=a
    B
    模式2=a\nb
    
    但是,通常使用原始字符串,因为在某些情况下,您可能需要,
    r'\1'
    引用回捕获组1,而
    '\1'
    将匹配
    '\x01'