LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-25-2011, 02:30 PM   #1
aiman
LQ Newbie
 
Registered: Apr 2007
Posts: 14

Rep: Reputation: 0
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
 
Old 08-25-2011, 03:00 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,599

Rep: Reputation: 516Reputation: 516Reputation: 516Reputation: 516Reputation: 516Reputation: 516
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.
 
Old 08-25-2011, 04:00 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,962
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Or with sed
Code:
echo abcdqa-asd-sd.local | sed -r 's/^[^-]+-([^.]+).*/\1/'
asd-sd

Last edited by Tinkster; 08-25-2011 at 06:39 PM.
 
Old 08-25-2011, 05:52 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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.)
 
Old 08-25-2011, 11:21 PM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
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

Last edited by kurumi; 08-25-2011 at 11:22 PM.
 
Old 08-26-2011, 12:15 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
Awk alternative:
Code:
echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$3}' OFS="-"
 
Old 08-26-2011, 12:24 AM   #7
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by grail View Post
Awk alternative:
Code:
echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$3}' OFS="-"
Nice. But may not work if there are multiple hyphens.....
 
Old 08-26-2011, 01:45 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
In bash, using full regex:

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

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

echo "${BASH_REMATCH[1]}"
 
Old 08-26-2011, 02:01 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
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="-"
 
Old 08-26-2011, 04:17 AM   #10
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by grail View Post
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 )
 
Old 08-26-2011, 04:22 AM   #11
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by David the H. View Post
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 )
 
Old 08-26-2011, 06:28 AM   #12
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,412

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
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)}'
 
  


Reply

Tags
regex


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 01:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 08:08 PM
Regex ganninu Programming 8 08-07-2010 01:14 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
Need a regex, I suck at regex's d3funct Programming 4 02-25-2002 08:28 PM


All times are GMT -5. The time now is 05:09 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration