d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Python Regular Expressions
12Next
Add Reply New Topic New Poll
Member
Posts: 36,366
Joined: Mar 13 2007
Gold: 18,943.50
Trader: Trusted
Apr 22 2015 01:37pm
Anyone good with these? Need a little help writing a script.

Thanks in advance :)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 22 2015 06:02pm
post it.

is your problem with regex in general or regex in python? if the latter, provide your vanilla regex and i'll try to convert it for you.

this should be helpful:
https://regex101.com/#python
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2015 03:46pm
Do not pm me. stick to the topic.

Quote (Floozy @ Apr 23 2015 05:35pm)
In regards to my thread: http://forums.d2jsp.org/topic.php?t=72530763&f=257

Here's the scenario:
Okay. Basically... I have to read in an input text file containing some zip codes, see if they are valid (either 9 digit or 5 digit) and print out the result of each.
i.e. let's say the zipcode is: 3896*

The output would be: Error - Invalid Zip Code - 3896*

Or... the zip code is: 32825
The output would be: Match - Valid Zip Code - 32825

Think you could help?


this regex will check if it's 5 or 9 digits.

^([0-9]{5}|[0-9]{9})$

loop through the file pulling each entry (i assume it's one per line) and check it against the regex.

see my earlier link for explanation of the regex

This post was edited by carteblanche on Apr 23 2015 03:47pm
Member
Posts: 36,366
Joined: Mar 13 2007
Gold: 18,943.50
Trader: Trusted
Apr 23 2015 04:01pm
Code
import re

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

string = input
pattern = '^([0-9]{5}|[0-9]{9})$'
m = re.match(pattern, string)

for eachLine in fileObject:
if m is not None:
print("Match found with: ", string, "\n")
else: print("Error - no match with:", string, "\n")
fileObject.close()


Not sure how to pass in the fileObject as the parameter for 're.match' as it doesn't work. Any tips?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2015 04:08pm
don't pass in the file. read the file as string, then loop through the file, iterating over each token, then check each token with the regex.
Member
Posts: 36,366
Joined: Mar 13 2007
Gold: 18,943.50
Trader: Trusted
Apr 23 2015 04:29pm
Quote (carteblanche @ Apr 23 2015 06:08pm)
don't pass in the file. read the file as string, then loop through the file, iterating over each token, then check each token with the regex.


How do you read in the file as a string? :unsure:
Sry, I'm a Python noob!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2015 04:39pm
Quote (Floozy @ Apr 23 2015 06:29pm)
How do you read in the file as a string? :unsure:
Sry, I'm a Python noob!


it's totally understandable that you wouldn't have thought of the idea, but after i introduce you to the idea, it's up to you to research it. whether or not you know python doesn't matter much.

http://lmgtfy.com/?q=python+read+file+into+string

now spend some time trying it, then post here with code if you're still running into problems.
Member
Posts: 36,366
Joined: Mar 13 2007
Gold: 18,943.50
Trader: Trusted
Apr 23 2015 04:48pm
Quote (carteblanche @ Apr 23 2015 06:39pm)
it's totally understandable that you wouldn't have thought of the idea, but after i introduce you to the idea, it's up to you to research it. whether or not you know python doesn't matter much.

http://lmgtfy.com/?q=python%20read%20file%20into%20string

now spend some time trying it, then post here with code if you're still running into problems.


Looked through everything I could find, actually Googled something similar. Most, if not all, had the file names hard-coded.
Code
import re

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

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

if m is not None:
print("Match found with: ", string, "\n")
else:
print("Error - no match with: ", string, "\n")
fileObject.close()


I ran it and it didn't stop running. Anything stick out about this? I figured string = string is wrong, but I don't know what to replace the value with.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 23 2015 05:01pm
Quote (Floozy @ Apr 23 2015 06:48pm)
Looked through everything I could find, actually Googled something similar. Most, if not all, had the file names hard-coded.

so just replace their hard coded string with your variable "fileName".

Member
Posts: 36,366
Joined: Mar 13 2007
Gold: 18,943.50
Trader: Trusted
Apr 26 2015 11:55am
Still need some help with this, struggling to understand if my logic is correct. lol
Go Back To Homework Help Topic List
12Next
Add Reply New Topic New Poll