Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

Regex for matching alphabet, numbers and special charters while looping in python

I am trying to find words and print using below code. Everything is working perfect but only issue is i am unable to print the last word(which is number).

words = ['Town of','Block No.','Lot No.','Premium (if any) Paid ']

import re
for i in words:
    y = re.findall('{} ([^ ]*)'.format(i), textfile)
    print(y)

Text file i working with:

textfile = """1, REBECCA M. ROTH , COLLECTOR OF TAXES of the taxing district of the
township of MORRIS for Six Hundred Sixty Seven dollars andFifty Two cents, the land
in said taxing district described as Block No. 10303 Lot No. 10 :
and known as 239 E HANOVER AVE , on the tax Taxes For: 2012
Sewer

Assessments For Improvements

Total Cost of Sale 35.00
Total
Premium (if any) Paid 1,400.00 """

Would like to know where am i making mistake. Any suggestion is appreciated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A couple of issues:

  1. As others have mentioned, you need to escape special characters like parentheses ( ) and dots .. Very simply, you can use re.escape
  2. Another issue is the trailing space in Premium (if any) Paid (it's trying to match two spaces instead of one as you're also checking for a space in your regex {} ([^ ]*))

You should instead change your code to the following:

See working code here

words = ['Town of','Block No.','Lot No.','Premium (if any) Paid']

import re
for i in words:
    y = re.findall('{} ([^ ]*)'.format(re.escape(i)), textfile)
    print(y)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...