d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Python Regular Expressions
Prev12
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 26 2015 12:17pm
is it still running forever? does that mean it's printing out thousands of "match found" and "error no match"?

if so, add a print / debug statement so you see what "eachLine" holds.
Member
Posts: 36,366
Joined: Mar 13 2007
Gold: 18,943.50
Trader: Trusted
Apr 26 2015 12:39pm
Quote (carteblanche @ Apr 26 2015 02:17pm)
is it still running forever? does that mean it's printing out thousands of "match found" and "error no match"?

if so, add a print / debug statement so you see what "eachLine" holds.


Negative. I changed the program a bit:

Code
import re

fileName = input('Please enter the name of the file containing the input zicpcodes: ')
fileObject = open(fileName, 'r')
string = fileObject

for eachLine in fileObject:
string = string
pattern = '^([0-9]{5}|[0-9]{9})$'
m = re.match(pattern, string)

with open(fileName) as input_file:
for i, line in enumerate(input_file):
if m is not None:
print("Match found with: ", string, "\n")
else:
print("Error - no match with: ", string, "\n")
fileObject.close()


Basically, I think the logic is right as it will iterate through each line and check the pattern against each string, but I'm just not sure what to set "string" equal to, hence it's just string = string.
"re" requires the pattern and string buffer, but I don't know what to use as the string. I want to use the strings/lines in the file, but setting it equal to fileObject also doesn't work.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 26 2015 12:58pm
Quote (Floozy @ Apr 26 2015 02:39pm)

Basically, I think the logic is right as it will iterate through each line and check the pattern against each string, but I'm just not sure what to set "string" equal to, hence it's just string = string.

conceptually, what do you think "string" should hold? what do you feel is the difference between "string" and "eachLine"? print out "eachLine" if you're not sure what it holds.

This post was edited by carteblanche on Apr 26 2015 01:14pm
Member
Posts: 36,366
Joined: Mar 13 2007
Gold: 18,943.50
Trader: Trusted
Apr 26 2015 01:13pm
Quote (carteblanche @ Apr 26 2015 02:58pm)
conceptually, what do you think "string" should hold? what do you feel is the difference between "string" and "eachLine"? print out "eachLine" if you're not sure what it holds.


Got it. Thank you for the help! :hug:
Member
Posts: 27,942
Joined: Nov 6 2006
Gold: 55,141.00
Apr 26 2015 04:14pm
Quote (carteblanche @ Apr 26 2015 02:58pm)
conceptually, what do you think "string" should hold? what do you feel is the difference between "string" and "eachLine"? print out "eachLine" if you're not sure what it holds.



just wana say thanks for helping people :D

it is a nice thing to do!
Go Back To Homework Help Topic List
Prev12
Add Reply New Topic New Poll