LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-05-2008, 07:25 AM   #1
sharath.bv
LQ Newbie
 
Registered: Feb 2008
Posts: 17

Rep: Reputation: 0
Post " Regular Expression - - -help"


Hi all,

I need a regular expression for searching a string in lines which satisfies the below conditions....

1) strings containing only digits and a single lowercase alphabet
2) digits are seperated by "," symbol
3) the string must not beging and end with the alphabet

ex: 0c1,2
2,4a5,6
 
Old 02-05-2008, 08:55 AM   #2
sharath.bv
LQ Newbie
 
Registered: Feb 2008
Posts: 17

Original Poster
Rep: Reputation: 0
Hi all,

Please check if the below syntax truely satisfies the given conditions...

$_=~/^[0-9]+(\,)*[a-z][0-9]+/
 
Old 02-05-2008, 08:57 AM   #3
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,191

Rep: Reputation: 105Reputation: 105
In what context are you using the regular expression? The options for regular expressions vary. They are more limited in grep than in, say, perl.

It seems that the following works in grep:

grep '\([0-9],\)\+[a-z],\([0-9],\)*[0-9]' filetosearch

However, that is the string embedded somewhere in a line. You could have a line like

a,b,c,1,2,3,a,2,3,b,c,d

and it would match. If you want to anchor it to the beginning and end of line, then

grep '^\([0-9],\)\+[a-z],\([0-9],\)*[0-9]$' filetosearch
 
Old 02-05-2008, 09:05 AM   #4
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,191

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by sharath.bv View Post
Hi all,

Please check if the below syntax truely satisfies the given conditions...

$_=~/^[0-9]+(\,)*[a-z][0-9]+/

You must try it and see. Create a sample file with lines of text, some of which have the pattern and some of which don't. Use your search pattern and see what it finds. Then you will see that it does not meet the conditions described in your first post.

Then try mine. I think it does, but there was some slight ambiguity in your conditions. I interpreted them to mean that commas separated all the elements.
 
Old 02-05-2008, 09:06 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
This sounds like homework, but your posting history says maybe not....

This is the kind of thing where you have to be very careful to test all possible outcomes. For example, where can the commas be? ..between the numbers or also between the letters and numbers? Also, how many numbers are possible?

Here's one example (used with SED)

sed -r 's/[0-9],?[0-9][a-z][0-9],?[0-9]/ZZZ/g' filename (Looks for the match and replaces all instances with "ZZZ".)

Translation: match any digit, followed by optional comma, another digit, a letter, another digit, optional comma, and another digit. Add one more comma in there, or a 2-digit number and it won't match.

To match any number of digits and optional commas, things will obviously get a bit trickier. The best way to proceed is to write something simple and test it. Then keep adding to it until it behaves the way you want.
 
  


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
Regular expression help. "?" m4a1rifle Linux - Newbie 25 03-05-2008 12:09 PM
Cant do "mount/umount" on USB flash drive as regular user Salgeras Slackware 5 11-01-2007 09:09 PM
Regular expression to match "^" then a number? PsychosisNode Linux - Newbie 1 01-14-2007 10:26 AM
"Out of range" Error for regular users but not root geekychic Linux - Hardware 2 04-01-2005 10:25 AM


All times are GMT -5. The time now is 01:45 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration