LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   New to regex, need help (http://www.linuxquestions.org/questions/programming-9/new-to-regex-need-help-899441/)

aiman 08-25-2011 03:30 PM

New to regex, need help
 
Hi guys am new to regex and am stuck, I need to extract text between first hyphen and first .(dot) from my hostname which is of the type

abcdqa-asd-sd.local

I want to get asd-sd through regex, the pattern of hostname always remains the same as stated.

Please help
Thanks

AlucardZero 08-25-2011 04:00 PM

Useful site: http://www.regular-expressions.info/

You should mention where you are using regexes for the best help. Basic REs are less than Extended REs are less than Perl Compatible REs.

Assuming you're using Perl you want
Code:

$string =~ /-(.+)\./;
print "$1\n";

Pretty simple regex.

Tinkster 08-25-2011 05:00 PM

Or with sed
Code:

echo abcdqa-asd-sd.local | sed -r 's/^[^-]+-([^.]+).*/\1/'
asd-sd


PTrenholme 08-25-2011 06:52 PM

Code:

$ echo abcdqa-asd-sd.local | gawk '{$0=gensub(/^[^-]+-([^.]+).*/,"\\1","n");print}'
asd-sd

And your could do gawk '{$0=gensub(/^[^-]+-([^.]+).*/,"\\1","n");print}' <input> > <output> to process a file. (Lines that don't match the expression would be copied verbatim to the output.)

kurumi 08-26-2011 12:21 AM

If you have Ruby(1.9+) or any language that supports splitting strings with limits.
Code:

string="abcdqa-asd-sd.local"
puts string.split("-",2)[-1].split(".")[0]

No regex needed. First split the string on "-". The parameter "2" means split on 2 fields. Then split on the last field ( the text you want is somewhere there) on "." and get the first element.

Or else, you can do it with bash

Code:

$ string="abcdqa-asd-sd.local"
$ echo ${string#*-}
asd-sd.local
$ string=${string#*-}
$ echo ${string%%.*}
asd-sd


grail 08-26-2011 01:15 AM

Awk alternative:
Code:

echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$3}' OFS="-"

kurumi 08-26-2011 01:24 AM

Quote:

Originally Posted by grail (Post 4453720)
Awk alternative:
Code:

echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$3}' OFS="-"

Nice. But may not work if there are multiple hyphens.....:)

David the H. 08-26-2011 02:45 AM

In bash, using full regex:

Code:

string="abcdqa-asd-sd.local"
re='^[^-]+-([^.]+)\.'

[[ "$string" =~ $re ]]

echo "${BASH_REMATCH[1]}"


grail 08-26-2011 03:01 AM

Quote:

Nice. But may not work if there are multiple hyphens
True enough, but simple change:
Code:

echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$(NF-1)}' OFS="-"

kurumi 08-26-2011 05:17 AM

Quote:

Originally Posted by grail (Post 4453783)
True enough, but simple change:
Code:

echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$(NF-1)}' OFS="-"

nice, but again, it will not work if there is string like "abcdqa-asd---jgsd.local". (of course, I am being picky here :) )

kurumi 08-26-2011 05:22 AM

Quote:

Originally Posted by David the H. (Post 4453765)
In bash, using full regex:

Code:

string="abcdqa-asd-sd.local"
re='^[^-]+-([^.]+)\.'

[[ "$string" =~ $re ]]

echo "${BASH_REMATCH[1]}"


nice, but that won't work with strings like "-abcdqa-asd---jgsd.local" (yes, I am being picky :) )

grail 08-26-2011 07:28 AM

Quote:

nice, but again, it will not work if there is string like "abcdqa-asd---jgsd.local". (of course, I am being picky here )
Please pick away as I always like a challenge :)
Code:

echo "abcdqa-asd---jgsd.local" | awk -F. '{print gensub(/[^-]*-/,"","1",$1)}'


All times are GMT -5. The time now is 12:48 PM.