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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-02-2008, 05:29 PM
|
#1
|
Member
Registered: Aug 2007
Location: Southern California
Distribution: Ubuntu 8.04 for my desktop & FreeBSD 7.0 for my server blade.
Posts: 31
Rep:
|
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.
|
|
|
04-02-2008, 06:30 PM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430
|
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] ?
|
|
|
04-02-2008, 08:08 PM
|
#3
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
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).
|
|
|
04-03-2008, 12:47 AM
|
#4
|
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:
|
"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.
|
|
|
04-03-2008, 11:18 AM
|
#5
|
HCL Maintainer
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450
Rep:
|
Quote:
Originally Posted by linuxmaveric
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
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.
|
|
|
04-03-2008, 06:22 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,430
|
Hopefully another part of your prog validates for real state codes, so you don't accept eg XX
|
|
|
04-04-2008, 01:57 AM
|
#7
|
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:
|
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.
|
|
|
All times are GMT -5. The time now is 06:54 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|