简体   繁体   English

re.match(r'。*',string)是否有限制?

[英]Does re.match(r'.*', string) have a limit?

#! /usr/bin/python3

import re

my_string = 'This is the string to test.  It has several Capitalized words.  My name is Robert, and I am learning pYthon.'
result = re.match(r'.*', my_string)
result.group(0)

print(result)

Forgive me for any issues I create posting this. 原谅我在发布此帖子时遇到的任何问题。 I am a total noob. 我完全是菜鸟。 I am trying to figure out why it is that when I run the above code, I get the follow results and not the full string. 我试图弄清楚为什么是这样,当我运行上述代码时,我得到了以下结果,而不是完整的字符串。

<_sre.SRE_Match object; span=(0, 108), match='This is the string to test.  It has several Capit>

Thanks in Advance. 提前致谢。

You are printing result, not result.group(0). 您正在打印结果,而不是result.group(0)。 Just do 做就是了

print(result.group(0))

and you will see the whole string. 您将看到整个字符串。

This is a quirk of printing a regex match object. 这是打印正则表达式匹配对象的怪癖。 If you look in the span attribute of the object, the match goes from char 0 to the last char of your string (108), and if you print result[0] as @sergio stated, you get the whole string 如果查看对象的span属性,则匹配从字符0到字符串的最后一个字符(108),并且如果按照@sergio的说明打印result[0] ,则会得到整个字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM