Regex: Extract substring between 2 separator


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regex: Extract substring between 2 separator
# 1  
Old 10-18-2012
Regex: Extract substring between 2 separator

Hi

Input:
Code:
aa-bb-cc-dd.ee.ff.gg

Output:
Code:
dd

I want to get the word after the last '-' until the first dot

I have tried with regex lookbehind and lookahead like this:
Code:
(?<=-).*(?=\.)

but his returns too much
Code:
bb-cc-dd.ee.ff

# 2  
Old 10-18-2012
what tool are you using?
sh/awk/sed/perl/????
# 3  
Old 10-18-2012
I am looking for a general solution if possible. But grep/java will be fine.
# 4  
Old 10-18-2012
Code:
echo 'bb-cc-dd.ee.ff' | sed 's/.*-\([^.][^.]*\).*/\1/'

# 5  
Old 10-18-2012
Code:
print 'bb-cc-dd.ee.ff' | sed -n 's/.*-\([^.]*\)\..*/\1/p'

This matches all characters up to a '-', then sets a reference to any character that is not a period, followed by zero or more characters
that are not a period, until a period is found, followed by anything else, then prints the part saved in the reference.
Bottom line, it prints all characters between the last '-' and the following '.'.

Last edited by gary_w; 10-18-2012 at 08:34 PM.. Reason: explanation of the regex
# 6  
Old 10-18-2012
Quote:
Originally Posted by vgersh99
Code:
echo 'bb-cc-dd.ee.ff' | sed 's/.*-\([^.][^.]*\).*/\1/'

So the steps is:

1. all characters until the last -
Code:
.*-

2. Create a backreference which does not match a dot
Code:
\([^.][^.]*\)

3. from the first dot to the rest of the line
Code:
.*

4. Replace everything from step 1-3 with the backreference value from step 2
# 7  
Old 10-18-2012
Quote:
Originally Posted by chitech
So the steps is:

1. all characters until the last -
Code:
.*-

2. Create a backreference which does not match a dot
Code:
\([^.][^.]*\)

3. from the first dot to the rest of the line
Code:
.*

4. Replace everything from step 1-3 with the backreference value from step 2
pretty much...
depending how your expected patterns will be, this may or may not be what you want, e.g. xx-zz-ww.bb-cc-dd.ee.ff might not produce what you want and might want to 'tighten up' greediness of the regex.
This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell Extract substring

Hi all, Please, i'd like to extract string just before '.fr'. Here is some lines of my file: g-82.text.text1.fr.worker1 g-xx.yyyyyy.zzzz.fr.worker2 i'd like to extract this text: g-82.text.text1 g-xx.yyyyyy.zzzz Please, which command i have to use in my script shell ? ... (16 Replies)
Discussion started by: chercheur111
16 Replies

2. Shell Programming and Scripting

Extract a substring from a file

Hello, A question please. A have a file that contains a string. Ex: AAAABBCCCCCDDEEEEEEEEEEFF I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file. From position 5, 2 caracters (ex:"BB") and from position 25, 2 caracters (ex:"FF") in a file. Could anoyone help me... (3 Replies)
Discussion started by: nolo41
3 Replies

3. Shell Programming and Scripting

Extract substring in a file

Hello, A question please. A have a file that contains a string. Ex: AAAABBCCCCCDDEEEEEEEEEEFF I'd want to recover 2 substrings, 'BB' and 'FF' and then leave them in a new file. Could anoyone help me please? Thanks in advance (3 Replies)
Discussion started by: nolo41
3 Replies

4. Shell Programming and Scripting

How to extract a substring from a string

Hi, I have an input string say for example: ABC,DEF,IJK,LMN,...,XYZ The above string is comma delimited. Now I have to extract the last part after the comma i.e. XYZ. :b: (3 Replies)
Discussion started by: bghosh
3 Replies

5. Shell Programming and Scripting

awk with multiple regex and substring

Hi Experts, I have a file on which i want to print the line which should match following criterias. Line should not start with 0 or 9 and Line should start with 1 and ( 576th character should not be 1 or 2 or 576-580 postion should not be NIPPF or CDIPB or 576-581 postion should... (2 Replies)
Discussion started by: millan
2 Replies

6. Solaris

Extract substring from a string

i have srtring i.e. "NAME,CLASS,AGE" (length of string is not constant) and from this string i've extract each word delimited by "," (comma). INPUT: "NAME,CLASS,AGE" OUTPUT: NAME CLASS AGE how can i do that? i have tried some string manipulation function like... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

7. Shell Programming and Scripting

Extract a substring.

I have a shell script that uses wget to grab a bunch of html from a url. URL_DATA=`wget -qO - "$URL1"` I now have a string $URL_DATA that I need to pull a substring out of..say I had the following in my string <p><a href="/scooby/929011567.html">Dog pictures check them out! -</a><font... (3 Replies)
Discussion started by: shellpower
3 Replies

8. Shell Programming and Scripting

Need Help... to extract the substring

> tnsping $TWO_TASK | grep HOST Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.12.10.212)(PORT = 1540)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = OMTST15))) I want to extract like this HOST = 10.12.10.212 PORT = 1540 SERVICE_NAME = OMTST15 I... (4 Replies)
Discussion started by: dashok.83
4 Replies

9. Shell Programming and Scripting

Sed extract substring on (OS X)

On OS 10.4.11 I have filenames like: 670711 SA T2 v1-1_DS_EF.doc CT_670520 AM T1 v1-2_DS_EF.doc CT_670716 - 2 SA T4 v1-2_DS_EF.doc CT_670713 SA T3 v1-1_DS_EF.doc 670421 PA DYP1 v1-1_DS_EF.doc CT_670425 PA DYP2 v1-1_DS_EF.doc CT_670107 RA T3 v1-2_DS_EF.doc CT_670521 AM T2 v1-2_DS_EF.doc... (3 Replies)
Discussion started by: mlommel
3 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question