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
如何通过使用Python3使用括号中有空格的regexp_Python_Regex_Whitespace_Brackets - Fatal编程技术网

如何通过使用Python3使用括号中有空格的regexp

如何通过使用Python3使用括号中有空格的regexp,python,regex,whitespace,brackets,Python,Regex,Whitespace,Brackets,我的代码如下: import re s = """ <contentID>1""" reg = re.compile("(.|\n)+<contentID>1.*") m = reg.fullmatch(s) print(m) reg = re.compile("[.\n]+<contentID>1.*") m = reg.fullmatch(s) print(m) 重新导入 s=”“” 1""" reg=重新编译((.|\n)+1.*) m=注册

我的代码如下:

import re

s = """
    <contentID>1"""
reg = re.compile("(.|\n)+<contentID>1.*")
m = reg.fullmatch(s)
print(m)
reg = re.compile("[.\n]+<contentID>1.*")
m = reg.fullmatch(s)
print(m)
重新导入
s=”“”
1"""
reg=重新编译((.|\n)+1.*)
m=注册完全匹配(s)
打印(m)
reg=重新编译(“[。\n]+1.*”)
m=注册完全匹配(s)
打印(m)

似乎正则表达式
[.\n]
不起作用,但
(.|\n)
起作用。为什么?在这种情况下,当使用括号时,如何编写RegExp?

而不是匹配换行符或文字点字符的
[.\n]
,使用
re.DOTALL
re.S
使
也能匹配换行符:

reg = re.compile(".*<contentID>1.*", re.DOTALL)
m = reg.fullmatch(s)
print(m)
reg=re.compile(“%1.*”,re.DOTALL)
m=注册完全匹配(s)
打印(m)

另见:

[]

用于指示一组字符。集合中:

  • 特殊角色在场景中失去了特殊意义。例如,
    [(++*)]
    将匹配任何文本字符
    +
    *
    ,或


    如果不使用
    fullmatch
    而使用
    search
    ,则只需使用
    reg=re.compile(“1”)
    如果s中的“1”输入字符串和预期结果是什么?
    [.]
    匹配文字点,而不是任何字符而是换行符。切勿使用
    “(.|\n)”
    ,请将
    re.S
    一起使用。在这种情况下使用括号时,您所说的RegExp是什么意思,以及如何编写RegExp?