Question on Regular Expression


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question on Regular Expression
# 8  
Old 05-16-2008
Thanks Radoulov
I was looking for only simple RE string not perl. something like:
[^\n\r]*/([^/\n\r]*)/[^/\n\r]*
# 9  
Old 05-17-2008
Quote:
Originally Posted by Katkota
[...]
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?
# 10  
Old 05-17-2008
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.
# 11  
Old 05-18-2008
Quote:
Originally Posted by Katkota
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?
# 12  
Old 05-18-2008
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
# 13  
Old 05-18-2008
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 11:27 AM..
# 14  
Old 05-18-2008
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"
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Regular expression question

Hi guys, I need a help with a query. Basically i want to know the difference between (0+01)* and ((0+01)*)* . It seems whatever string can be generated by the first RE can also be generated by second and they should essentially be same. Am i missing something? (1 Reply)
Discussion started by: srkmish
1 Replies

2. UNIX for Dummies Questions & Answers

Regular Expression Question

Hello, I'm trying to rename a bunch of files that were named incorrectly. I know a little about regular expressions but I'm not very good at them. Here is the image of the file names: http://i47.tinypic.com/np2gxi.jpg I'm trying to change the 20111116 at the beginning to 20101116 for all... (2 Replies)
Discussion started by: nastyn8
2 Replies

3. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

4. UNIX for Dummies Questions & Answers

a question about a regular expression

I like to loop a list of files which named file1, file2, file3, file4, etc if I like to loop them all over for f in file1, file2, file3, file4 do echo "processing" $f done how to use a regular expression to loop file$i instead? Thank you. (4 Replies)
Discussion started by: ksgreen
4 Replies

5. Shell Programming and Scripting

Easy Perl regular expression question

Hey all! what matching expression might I use to match all characters included in \W, EXCEPT < and > ? for example: @tokens=split(/ ???? /,$string); I've dubiously tried \W but this clips off the first letter of each "token", for some reason ... (2 Replies)
Discussion started by: applefat
2 Replies

6. UNIX for Dummies Questions & Answers

Regular Expression question

Folks; I have 3 questions & any help with them would be really appreciated: If i have a list of directories, for example: /fs/pas/2007/4/6/2634210/admdat/examin /fs/pas/2007/4/6/2634210/admdat2/stat /fs/pas/2007/4/6/2634210/admdat3/data /fs/pas/2007/4/6/2634210/im_2/0b.dcm Now; my... (6 Replies)
Discussion started by: Katkota
6 Replies

7. UNIX for Dummies Questions & Answers

question (regular expression related)

anyone knows what does this regular expression match for? \(3,\).*\1.*\1 (1 Reply)
Discussion started by: metalwarrior
1 Replies

8. Shell Programming and Scripting

Regular expression question

hi i need to wipe out something from giving path i have some thing like that : pwd | sed 's/.*foo//' it is working fine when I have path like : /blah/balh1/foo/moo so it erasing me all that comes before the foo including the foo but I have problem when I have dir by the name of... (7 Replies)
Discussion started by: umen
7 Replies

9. UNIX for Dummies Questions & Answers

Regular Expression Question

Hi - I am trying to ignore the following items from a list. lp0 lp11 lp12 lp14 The following code works fine, but I was wondering if there was a tidier way to write the lp regular expression? egrep -v "lp" Thanks in advance. (3 Replies)
Discussion started by: Krispy
3 Replies

10. Shell Programming and Scripting

question about regular expression

why does * highlight everything in it... shouldn't it only highlight capital letters? (0 Replies)
Discussion started by: brentdeback
0 Replies
Login or Register to Ask a Question