Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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中的多行正则表达式_Python_Regex - Fatal编程技术网

python中的多行正则表达式

python中的多行正则表达式,python,regex,Python,Regex,我想使我的代码更易于维护,所以我遇到了这个问题 会 与以下内容相同: re.compile(r'foo|bar')#blabla 还有这个: re.compile(r"""foo #some comments bar""") IdeaJ建议如下: re.compile(r'foo' r'bar') 我这里有几千个这样的“foobar” 我知道第三个可能会产生一些不必要的\w 但是其他的呢 我想要的只是一个正则表达式匹配foo或bar您可以

我想使我的代码更易于维护,所以我遇到了这个问题

与以下内容相同:

re.compile(r'foo|bar')#blabla
还有这个:

re.compile(r"""foo #some comments
               bar""")
IdeaJ建议如下:

re.compile(r'foo'
           r'bar')
我这里有几千个这样的“foobar”

我知道第三个可能会产生一些不必要的\w 但是其他的呢


我想要的只是一个正则表达式匹配foo或bar

您可以通过指定
re.VERBOSE
标志将注释放入正则表达式中

re.compile(r'''foo  # some comments
               |bar # some more comments
            ''', re.VERBOSE)

该标志的简写形式是
re.X

您可以通过指定
re.VERBOSE
标志将注释放入正则表达式中

re.compile(r'''foo  # some comments
               |bar # some more comments
            ''', re.VERBOSE)

该标志的简写形式是
re.X

在Python文档中找到了这个


在Python文档中找到了这个


如果在第一个子字符串后面加一个加号来连接字符串,那么第一个子字符串应该可以工作。也许可以使用string.join<代码>'|'。加入(r'foo',r'bar…)谢谢,但如果我得到更多呢?我必须在每行中添加一个“+”吗
r'foo'(LineBreak)+'bar'(LineBreak)+'foobar'
@erlc这是一种非常漂亮的对象使用方式!谢谢如果在第一个子字符串后面加一个加号来连接字符串,那么第一个子字符串应该可以工作。也许可以使用string.join<代码>'|'。加入(r'foo',r'bar…)谢谢,但如果我得到更多呢?我必须在每行中添加一个“+”吗
r'foo'(LineBreak)+'bar'(LineBreak)+'foobar'
@erlc这是一种非常漂亮的对象使用方式!谢谢我总是觉得有一个冗长的旗子的速记很有趣。我总是觉得有一个冗长的旗子的速记很有趣。
 re.compile("[A-Za-z_]"       # letter or underscore
            "[A-Za-z0-9_]*"   # letter, digit or underscore
            )