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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
08-25-2011, 02:30 PM
#1
LQ Newbie
Registered: Apr 2007
Posts: 12
Rep:
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
08-25-2011, 03:00 PM
#2
Senior Member
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,443
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.
08-25-2011, 04:00 PM
#3
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,627
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 .
08-25-2011, 05:52 PM
#4
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 3,666
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.)
08-25-2011, 11:21 PM
#5
Member
Registered: Apr 2010
Posts: 217
Rep:
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 .
08-26-2011, 12:15 AM
#6
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403
Awk alternative:
Code:
echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$3}' OFS="-"
08-26-2011, 12:24 AM
#7
Member
Registered: Apr 2010
Posts: 217
Rep:
Quote:
Originally Posted by
grail
Awk alternative:
Code:
echo "abcdqa-asd-sd.local" | awk -F"[-.]" '{print $2,$3}' OFS="-"
Nice. But may not work if there are multiple hyphens.....
08-26-2011, 01:45 AM
#8
Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,338
In bash, using full regex:
Code:
string="abcdqa-asd-sd.local"
re='^[^-]+-([^.]+)\.'
[[ "$string" =~ $re ]]
echo "${BASH_REMATCH[1]}"
08-26-2011, 02:01 AM
#9
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403
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="-"
08-26-2011, 04:17 AM
#10
Member
Registered: Apr 2010
Posts: 217
Rep:
Quote:
Originally Posted by
grail
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
)
08-26-2011, 04:22 AM
#11
Member
Registered: Apr 2010
Posts: 217
Rep:
Quote:
Originally Posted by
David the H.
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
)
08-26-2011, 06:28 AM
#12
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403
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)}'
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 02:38 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News