简体   繁体   中英

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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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