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 |
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 =~ /-(.+)\./; |
Or with sed
Code:
echo abcdqa-asd-sd.local | sed -r 's/^[^-]+-([^.]+).*/\1/' |
Code:
$ echo abcdqa-asd-sd.local | gawk '{$0=gensub(/^[^-]+-([^.]+).*/,"\\1","n");print}' |
If you have Ruby(1.9+) or any language that supports splitting strings with limits.
Code:
string="abcdqa-asd-sd.local" Or else, you can do it with bash Code:
$ string="abcdqa-asd-sd.local" |
Awk alternative:
Code:
echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$3}' OFS="-" |
Quote:
|
In bash, using full regex:
Code:
string="abcdqa-asd-sd.local" |
Quote:
Code:
echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$(NF-1)}' OFS="-" |
Quote:
|
Quote:
|
Quote:
Code:
echo "abcdqa-asd---jgsd.local" | awk -F. '{print gensub(/[^-]*-/,"","1",$1)}' |
All times are GMT -5. The time now is 08:30 AM. |