Python正则表达式字符串匹配? [英] Python regex string matching?

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

问题描述

我花了很长时间试图将我的 javascript regex 经验转移到 Python.

I'm having a hell of a time trying to transfer my experience with javascript regex to Python.

我只是想让它起作用:

print(re.match('e','test'))

...但它打印无.如果我这样做:

...but it prints None. If I do:

print(re.match('e','est'))

它匹配...是否默认匹配字符串的开头?当它匹配时,我如何使用结果?

It matches... does it by default match the beginning of the string? When it does match, how do I use the result?

我如何进行第一个匹配?有比 python 站点提供的更好的文档吗?

How do I make the first one match? Is there better documentation than the python site offers?

推荐答案

re.match 隐式地将 ^ 添加到正则表达式的开头.换句话说,它只匹配字符串的开头.

re.match implicitly adds ^ to the start of your regex. In other words, it only matches at the start of the string.

re.search 将在所有位置重试.

一般来说,我建议使用 re.search 并在需要时明确添加 ^.

Generally speaking, I recommend using re.search and adding ^ explicitly when you want it.

http://docs.python.org/library/re.html

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

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