在下面的示例模块中。该模式仅匹配字符串的第一部分。如何使其匹配并显示与模式匹配的所有子串?
filtered ='\n\rweakmoves (1835) seeking 5 0 unrated blitz ("play 89" to respond)\n\r\n\rGuestKCCZ (++++) seeking 15 0 unrated standard ("play 91" to respond)\n\r\n\rGuestKKKP (++++) seeking 3 0 unrated blitz f ("play 59" to respond)\n\r'
rgxseeking =re.compile(r'^\S+\s\(([\d+-]+)\)\s+seeking\s+(\d+\s\d+)\s+(\S{1,7})\s(\S{1,9})(\s\[\S+\])?((\s[fm])?)((\s[fm])?)\s\("play\s\d{1,3}\"\sto\srespond\)$',re.MULTILINE)
seeking= re.search(rgxseeking,filtered)
if seeking:
print seeking.group()
/////////////////////////////////////////////// ///////////// 这是来自服务器的传入输入
FICS.username=username ="guest"
FICS.password=password =""
self.logged = logged = False
self.username = username = False
self.closed = closed = False
self.parsetime = parsetime = 5000
self.timeout = timeout = 5000
try:
self.host = "69.36.243.188"
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, timeout))
self.sock.settimeout(2)
self.sock.setblocking(0)
except Exception as inst:
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # __str__ allows args to printed directly
从此正则表达式中收集的数据
#for Analysis of string format
rgxunwanted = '.*'
unwanted = re.findall(rgxunwanted, raw, re.DOTALL)
if unwanted:
print unwanted
上述模块返回的一大块数据:
["earn how to ask for help on the help channel.\n\r type 'help register' to learn how to get a registered account.\n\r type 'help interfaces' for information on getting a graphical interface.\n\r type 'showadmins' to see which admins (FICS administrators) are logged on.\n\r type 'set shout 1' to see shouts (only registered users may shout).\n\r type 'set seek 0' if receiving ads for matches bothers you.\n\r type 'set open 0' if you do not want to be challenged to a chess match.\n\r type 'set silence 1' if you wish to have quiet play.\n\r type 'getgame' to get a blitz chess game.\n\r\n\rSpecial channels for guests:\n\r type 'tell 4 <message>' if you need help\n\r type 'tell 53 <message>' to chat with other users\n\r\n\r A quick guide is available on our web page: http://www.freechess.org\n\r as are facilities to register, connect via a java interface or download\n\r interfaces, so that you get a graphical board and can use the mouse.\n\r\n\rNOTE: Guests with foul or abusive names may be 'nuked' without warning!!!\n\r\n\rfics% \n\rGuestWW", '']
['DS (++++) seeking 10 0 unrated blitz [white] ("play 26" to respond)\n\rfics% \n\rROBOadmin(*)(TD) tells you: Welcome to FICS - the Free Internet Chess Server. \n\r\\ Please visit our homepage at http://www.freechess.org. From there you can \n\r\\ register or download a graphical interface. You can get help by asking a \n\r\\ question in channel 4 - use "tell 4 My question is...". Or you can get \n\r\\ help from one of our service representives; these are marked (SR) and can \n\r\\ be seen with "showsrs"\n\rfics% \n\rGuestRCBT (++++) seeking 15 0 unrated standard ("play 121" to respond)\n\rfics% \n\rGuestHPZR (++++) seeking 5 5 unrated blitz ("play 2" to respond)\n\rfics% \n\rGuestBGVC (++++) seeking 5 0 unrated blitz ("play 95" to respond)\n\rfics% \n\rGuestRGYQ (++++) seeking 3 0 unrated blitz ("play 83" to respond)\n\rfics% \n\rGuestCSXP (++++) seeking 3 2 unrated blitz ("play 103" to respond)\n\rfics% ', '']
['\n\rGuestXQHW (++++) seeking 10 0 unrated blitz f ("play 11" to respond)\n\rfics% ', '']
['\n\rGuestPDXX (++++) seeking 10 0 unrated blitz f ("play 35" to respond)\n\rfics% ', '']
['\n\rGuestBGYP (++++) seeking 2 12 unrated blitz m ("play 22" to respond)\n\rfics% \n\rGuestZKGP (++++) seeking 5 0 unrated blitz ("play 52" to respond)\n\rfics% \n\rGuestMFLK (++++) seeking 10 0 unrated blitz ("play 56" to respond)\n\rfics% \n\rGuestZSTH (++++) seeking 13 5 unrated standard ("play 59" to respond)\n\rfics% ', '']
['\n\rGuestSHQS (++++) seeking 3 0 unrated blitz f ("play 65" to respond)\n\rfics% \n\rGuestXPSS (++++) seeking 6 5 unrated blitz f ("play 62" to respond)\n\rfics% ', '']
最后,当我运行这个正则表达式时,它只返回第一个“搜索”子字符串,而不是消息中的所有子字符串。 raw是来自我上面粘贴的服务器的未格式化消息。请注意我添加了\ s。我最后还尝试了[s *和[\ n \ r]] [/ n]并用^ $封闭正则表达式并在多行中运行:
# \n\r \n
#Working - ongoing
#GuestFCKN (++++) seeking 5 0 unrated blitz [white] f ("play 11" to respond)
#GuestQWHB (++++) seeking 15 5 unrated standard [white] ("play 122" to respond)
#GuestQWHB (++++) seeking 15 5 unrated standard f m ("play 122" to respond)
#GuestQWHB (++++) seeking 15 5 unrated standard f ("play 122" to respond)
#GuestQWHB (++++) seeking 15 5 unrated standard ("play 122" to respond)
rgxseeking =re.compile(r'\s\S+\s\(([\d+-]+)\)\s+seeking\s+(\d+\s\d+)\s+(\S{1,7})\s(\S{1,9})(\s\[\S+\])?((\s[fm])?)((\s[fm])?)\s\("play\s\d{1,3}\"\sto\srespond\)',re.MULTILINE)
seeking= re.search(rgxseeking,filtered)
if seeking:
print seeking.group()
答案 0 :(得分:0)
seeking
返回None
,因此在正则表达式的开头添加前瞻断言(?=\s*)
。固定正则表达式在
rgxseeking =re.compile(r'^(?=\s*)\S+\s\(([\d+-]+)\)\s+seeking\s+(\d+\s\d+)\s+(\S{1,7})\s(\S{1,9})(\s\[\S+\])?((\s[fm])?)((\s[fm])?)\s\("play\s\d{1,3}\"\sto\srespond\)$',re.MULTILINE)
从服务器返回的原始数据可以使用字符串'\n\r'
拆分。然后你将有以下代码
if raw:
for line in raw.split('\n\r'):
seeking = re.search(rgxseeking, line)
if seeking:
print seeking.group()