簡體   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)

原諒我在發布此帖子時遇到的任何問題。 我完全是菜鳥。 我試圖弄清楚為什么是這樣,當我運行上述代碼時,我得到了以下結果,而不是完整的字符串。

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

提前致謝。

您正在打印結果,而不是result.group(0)。 做就是了

print(result.group(0))

您將看到整個字符串。

這是打印正則表達式匹配對象的怪癖。 如果查看對象的span屬性,則匹配從字符0到字符串的最后一個字符(108),並且如果按照@sergio的說明打印result[0] ,則會得到整個字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM