最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python的正则re的search查找不到值

Python crifan 3964浏览 0评论

折腾:

【已解决】Python的PyMongo中更新替换一个记录值

期间,想要写个正则去支持:

5c1c6323127588257d56949e
http://127.0.0.1:34800/image/5c1c6323127588257d56949e/white%20and%20brown%20chopsticks.png

查找出:

5c1c6323127588257d56949e

用代码:

matchedId = re.search("(/[a-z]+/)?(?P<idStr>\w{20, 30})/?", idStrOrUrl)

试了多次,都找不到值,返回都是None:

后来把\w换成其他的[a-z0-9]之类的,也都不行。

参考了很多资料:

python re named group

7.2. re — Regular expression operations — Python 2.7.15 documentation

“{m,n}

Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 ‘a’ characters. Omitting m specifies a lower bound of zero, and omitting n specifies an infinite upper bound. As an example, a{4,}b will match aaaab or a thousand ‘a’ characters followed by a b, but not aaab. The comma may not be omitted or the modifier would be confused with the previously described form.”

最后才意识到:

原来是named group中的限定数量:

{m,n}

中间不能有多余空格,即:

{20, 30}

应该改为:

{20,30}

即:

matchedId = re.search("(/\w{3,10}/)?(?P<idStr>[a-z0-9]{20,30})/?", idStrOrUrl)

PyCharm的语法高亮立刻就显示出20和30所对应的数字的颜色了,也就可以找到要的值了:

也才注意到:

之前的:

{20, 30}

只是会被识别为字符串,而显示出普通的字符串的颜色:

【总结】

Python中的正则re中的特殊字符中的,表示数量限制的:

{m,n}

m和n和逗号中间不能有空格

否则会导致无法工作,找不到要的数据。

应该把:

matchedId = re.search("(/\w{3,10}/)?(?P<idStr>[a-z0-9]{20, 30})/?", idStrOrUrl)

改为:

matchedId = re.search("(/\w{3,10}/)?(?P<idStr>[a-z0-9]{20,30})/?", idStrOrUrl)

才能从:

5c1c6323127588257d56949e
http://127.0.0.1:34800/image/5c1c6323127588257d56949e/white%20and%20brown%20chopsticks.png

搜索到此处要的值:

5c1c6323127588257d56949e

转载请注明:在路上 » 【已解决】Python的正则re的search查找不到值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.181 seconds, using 22.06MB memory