Python正则表达式与行开始不匹配 [英] Python regex does not match line start

查看:45
本文介绍了Python正则表达式与行开始不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些数据的文本文件:

I have a text file with some data:

... 
DATA_ARRAY Some[] =
{
...
};

我有一个像这样的 python 2.7 正则表达式:

and I have a python 2.7 regex like this:

regx = re.compile("^DATA_ARRAY Some\[\].*?};", re.DOTALL)
regmatch = re.search(regx, data)
print regmatch.group(0)

问题是正则表达式不匹配任何东西(regmatch 是 None).如果我删除 ^ 那么它匹配得很好.

The problem is that the regex does not match anything (regmatch is None). If I remove the ^ then it matches just fine.

我在这里做错了什么?我想添加行开始搜索符号.

What am I doing incorrectly here? I would like to add the line beginning search symbol.

推荐答案

^ 检查字符串的开头.. 添加 re.MULTILINE 标志.

^ checks for start of the string.. add re.MULTILINE flag.

regx = re.compile("^DATA_ARRAY Some\[\].*?};", re.MULTILINE|re.DOTALL)

这篇关于Python正则表达式与行开始不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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