The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Regular Expression question Katkota UNIX for Dummies Questions & Answers 6 05-14-2008 12:23 PM
question (regular expression related) metalwarrior UNIX for Dummies Questions & Answers 1 02-03-2008 07:51 PM
Regular expression question umen Shell Programming and Scripting 7 11-21-2007 05:45 PM
Regular Expression Question Krispy UNIX for Dummies Questions & Answers 3 01-20-2006 06:36 AM
question about regular expression brentdeback Shell Programming and Scripting 0 11-14-2005 12:04 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #8 (permalink)  
Old 05-16-2008
Registered User
 

Join Date: Oct 2007
Posts: 83
Stumble this Post!
Thanks Radoulov
I was looking for only simple RE string not perl. something like:
[^\n\r]*/([^/\n\r]*)/[^/\n\r]*
Reply With Quote
Forum Sponsor
  #9 (permalink)  
Old 05-17-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,536
Stumble this Post!
Quote:
Originally Posted by Katkota View Post
[...]
I was looking for only simple RE string not perl. something like:
[^\n\r]*/([^/\n\r]*)/[^/\n\r]*
What's the reason? What are you trying to achieve?
Reply With Quote
  #10 (permalink)  
Old 05-17-2008
Registered User
 

Join Date: Oct 2007
Posts: 83
Stumble this Post!
I'm given a text files & i'm required to extract specific fields or directories from each line in the file and it must be extracted using regular expressions.
Reply With Quote
  #11 (permalink)  
Old 05-18-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,536
Stumble this Post!
Quote:
Originally Posted by Katkota View Post
I'm given a text files & i'm required to extract specific fields or directories from each line in the file and it must be extracted using regular expressions.
So what's wrong with my post?
Reply With Quote
  #12 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: Oct 2007
Posts: 83
Stumble this Post!
Nothing wrong with your post, i just can't use a code, it must be one line for each request with no perl, just one line of RE so if i apply this RE to the line i'm trying to extract the field from, it should produce the results i'm looking for.
To make my request more clear, I'm using this link to confirm the result by entering the string & the RE then i see the results:

Jakarta Regexp - Jakarta Regexp Applet
Reply With Quote
  #13 (permalink)  
Old 05-18-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
Regular expressions by themselves simply match something; they don't "extract". You can use them to identify strings which match a particular pattern; for example, whether the input has at least two slashes, and a string of non-slashes between those two slashes. However, the regular expression by itself does not as such allow you to extract part of the match (in the example, the string between the slashes). That's why you keep receiving replies which involve some sort of code -- it depends on the capabilities of the tool you use how you would use the regular expressions to extract any part of the match (in this thread as well as your original thread).

Thanks for the pointer to the Jakarta regex demo; the fact that you are using this makes a world of a difference.

If that tool allows for extracting a parenthesized subexpression, then your second problem can be solved, but the regular expression needs to look at (and, by extension, "extract") most or all of the context to know whether a particular slash is the last one in the test string.

So you can use /([^/]*)/[^/]*$ to tell whether there are two slashes before the end of the string, and if that is so, then the first parenthesized subexpression ($1 or \1 in many regex tools) will contain the parenthesized part between the slashes; but there is no way to construct a regular expression which alone picks out just the text between those two specific slashes. (The parentheses by themselves do not add anything to the regular expression, in this case; they are useful here only because of the side effect of capturing a substring of the match.)

If you relax the requirement (perhaps because none of your examples contain more than two slashes) then you can match (and "extract") a string between two slashes, but without reference to context, the regex engine will pick the first place in the string where it finds text between two slashes.

It appears that in all your examples, the first string between two slashes which is longer than three characters and which does not begin with a number is the parent directory; thus [^/0-9][^/][^/][^/][^/]* will coincidentally pick out that, but relying on this to work on a larger number of samples appears most precarious.

Last edited by Neo; 05-18-2008 at 07:27 AM.
Reply With Quote
  #14 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: Oct 2007
Posts: 83
Stumble this Post!
Thanks Era;
I'm sorry that i chose the wrong word to describe what i was looking for. I shouldn't say "extract", I should have said "match".
SO, i will make my questions below as clear as possible:

If i have these lines below
/fs/pas/2007/4/6/2634210/admdat/examin
/fs/pas/2007/4/6/2634210/admdat2/stat
/fs/pas/2008/2/3/2634210/admdat3/data
/fs/pas/2007/4/6/2634210/im_2/0b.dcm

I would like to create 2 REs to run it against any of these lines "strings" using Apache link Jakarta Regexp - Jakarta Regexp Applet , so the results would be:

1. One RE to match the directory before last such "admdat, admdat2, im_2"

2. Another RE to match the date in each string such "2007/4/6, 2008/2/3"
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:12 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0