Python中文
首页
教程
问答
标签
搜索
登录
注册
python正则表达式古怪
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我认为我可以使用正则表达式-但这让我感到困惑-我有一行python代码:</p> <pre class="lang-py prettyprint-override"><code>dependencies = re.findall( r"-- *depends *on *([^ ]*.*[^ ]) *$", script, re.MULTILINE) </code></pre> <p>这在以下情况下非常有效:</p> <pre><code>"-- depends on b " -> ["b"] "-- depends on b" -> ["b"] "--dependson green things \n-- depends on red things\nother stuff"" -> ["green things", "red things"] "-- depends on b \n-- depends on c" -> ["b", "c"] </code></pre> <p>但对我来说不起作用</p> <pre><code>"-- depends on b\n-- depends on c" -> ["b\n-- depends on c"] </code></pre> <p>我明白了,在换行之前$matches这个事实会有些奇怪,但我不明白的是如何修复正则表达式</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>它将“\n”换行符匹配为“非空格”,您可以这样修复它,例如:</p> <p><code> *depends *on *([^ \n]*.*[^ \n]) *$</code></p> <p>你可能真的想要这样的东西:</p> <p><code> \s*depends\s*on\s*(\S*.*\S)\s*$</code></p> <p><code>\s</code>表示“任何空间类型”,而<code>\S</code>表示任何非空间类型</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何合并多个PDF文件?
8 回答
如何合并多个xarray数据变量及其坐标?
10 回答
如何合并多个列中具有重复值的行
6 回答
如何合并多个唯一id
1 回答
如何合并多个图纸并使用图纸名称的名称重命名列名?
6 回答
如何合并多个字典并添加同一个键的值?(Python)
3 回答
如何合并多个搜索结果文件(pkl)以将它们全部打印在一起?
10 回答
如何合并多个数据帧
3 回答
如何合并多个数据帧并使用Pandas为假人添加列?
8 回答
如何合并多个数据帧并按时间戳排序
2 回答
如何合并多个数据帧的列表并用另一个lis标记每列
10 回答
如何合并多个数据框中的列
10 回答
如何合并多个文件?
5 回答
如何合并多个查询集?
4 回答
如何合并多个绘图?
3 回答
如何合并多个词典
9 回答
如何合并多个输入数据集(数据帧)?
5 回答
如何合并多条记录中拆分的文本行
4 回答
如何合并多索引列datafram
10 回答
如何合并多级(即多索引)数据帧?
8 回答