LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2008, 05:29 PM   #1
linuxmaveric
Member
 
Registered: Aug 2007
Location: Southern California
Distribution: Ubuntu 8.04 for my desktop & FreeBSD 7.0 for my server blade.
Posts: 31

Rep: Reputation: 15
Talking A simpler solution to my original post on regEx


Original quote "Can anyone help me figure out how to write a simple regular expression "using grep" that would search in files for a "city, state and zipcode" in the following order using commas to separate each field:

City, ST, 12345

It should search for any city and 2 capital letters that could represent a state ST and a zip code that can be any 5 numbers form [0-9]. No egrep or ruby just usning simple grep & regEx.



I created a simpler and easier solution for my problem that I originally posted. Using grep & regEx to find a address consisting of "City, State and zip" in a file. City could be anything, state always two capitals "ST", and a 5 digit zip.

Here is my basic solution:

grep ", [A-Z][A-Z] [0-9][0-9][0-9][0-9][0-9]" /filename

This would do the job nicely. But thanks everyone for the suggestions. They gave me the ideas to figure this out.

Last edited by linuxmaveric; 04-03-2008 at 12:59 AM.
 
Old 04-02-2008, 06:30 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430

Rep: Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787
You do realise that'll match any 2 alpha 'state' code ?
If you only want ST, you should make that explicit.
Looks a bit like you are accepting negative zip codes: [-0-9] ?
 
Old 04-02-2008, 08:08 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 79
What’s wrong with:
Code:
grep ', [A-Z]\{2\} [0-9]\{5\}' filename
Note that the following lines will match (perhaps undesirably):
Code:
Washington, DC 2000112345
, AL 36210
ATTENTION, ON 20080403 THE SYSTEM WILL BE DOWN!
(granted, unless you have all valid postal abbreviations off-hand, you will not be able to overcome the problem chrism01 mentions).
 
Old 04-03-2008, 12:47 AM   #4
linuxmaveric
Member
 
Registered: Aug 2007
Location: Southern California
Distribution: Ubuntu 8.04 for my desktop & FreeBSD 7.0 for my server blade.
Posts: 31

Original Poster
Rep: Reputation: 15
Smile "ST" was just an example of state :)

"ST" is just an example I used. State could be anything. Thanks for the reply.
RR.

You are correct a typo witjh [-0-9] "fixed" [0-9.

revised version.
grep ", [A-Z][A-Z] [0-9][0-9][0-9][0-9][0-9]" /filename

Thanks for the critique.

BTW. There is nothing wrong with your code. I was just trying to make it very simple in structure. Using basic regex.
RR.

Last edited by linuxmaveric; 04-04-2008 at 01:55 AM.
 
Old 04-03-2008, 11:18 AM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 79
Quote:
Originally Posted by linuxmaveric View Post
You are correct I should tweak [0-9] to be more specific and only include numbers bewtween 1 thru 9.
I don’t remember anyone saying this. The digit 0 is a valid for zip codes in the US. What chrism01 said is that originally, you had [-0-9] whereas you should have used [0-9]. What I said is that trailing digits are matched, instead of exactly 5.
Quote:
Originally Posted by linuxmaveric View Post
BTW. There is nothing wrong with your code. I was just trying to make it very simple in structure. Using basic regex.
Well, my code was in the form of a BRE (basic regular expression), which is different from an ERE (extended regular expression). Suit yourself.
 
Old 04-03-2008, 06:22 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430

Rep: Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787Reputation: 2787
Hopefully another part of your prog validates for real state codes, so you don't accept eg XX
 
Old 04-04-2008, 01:57 AM   #7
linuxmaveric
Member
 
Registered: Aug 2007
Location: Southern California
Distribution: Ubuntu 8.04 for my desktop & FreeBSD 7.0 for my server blade.
Posts: 31

Original Poster
Rep: Reputation: 15
Unhappy Sorry if I offended you :)

Well, my code was in the form of a BRE (basic regular expression), which is different from an ERE (extended regular expression). Suit yourself.

Sorry if I offended you. This is all very new to me and I'm just learning like everyone else here in the forum. No need to get touchy. Your code is sound. Take care.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A no other site URL on first post rule. Then I give my solution to this sound issue! bvip2003 LinuxQuestions.org Member Intro 1 12-13-2007 02:18 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Something Simpler than Tab Window Manager? Ariox Linux - Software 5 08-25-2005 06:55 AM
Quick regex problem, can't find solution R00ts Programming 3 05-25-2005 02:55 PM
simpler knoppix HD install...? questionasker Linux - Distributions 4 02-21-2004 04:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration