McDarren has asked for the wisdom of the Perl Monks concerning the following question:
I have a set of filenames that appear as follows:
The format of each filename is:hosta-sel-kr-1,my-domain,net.testa hostb-sel-kr-1,my-domain,net.testb hostc-sel-kr-1,my-domain,com.testa hostd-sel-kr-1,my-domain,com.testc hoste-sel-kr-1,my-domain,net.testxyz hosta-mel-au-1,my-domain,net.testabc hosta-mel-au-1,my-domain,net.testdef hostxyz.testabc someotherhost.someothertest
I need to extract the hostname and the test name from each filename, however:
So I have two questions:
#!/usr/bin/perl -w use strict; while (<DATA>) { my ($host, $test) = ($_ =~ m/ ( # Start first capture [\w\-]+ # One or more alphanum or hyphens (?: # non-capturing lookahead ,my-domain,com # Literal string )? # Make it optional ) # End of first capture (?: # non-capturing lookahead [\w\-,]+ # One or more alpanum or hyphens )? # Make it optional \. # A literal period ( # Start second capture [a-z]+ # One or more lowercase chars ) # End second capture /x) or print "Cannot parse $_\n" and next; print "Host:$host Test:$test\n"; } __DATA__ hosta-sel-kr-1,my-domain,net.testa hostb-sel-kr-1,my-domain,net.testb hostc-sel-kr-1,my-domain,com.testa hostd-sel-kr-1,my-domain,com.testc hoste-sel-kr-1,my-domain,net.testxyz hosta-mel-au-1,my-domain,net.testabc hosta-mel-au-1,my-domain,net.testdef hostxyz.testabc someotherhost.someothertest
Any advice would be greatly appreciated.
Thanks in advance,
Darren :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Regex: Capturing and optionally replacing
by ikegami (Patriarch) on Dec 08, 2005 at 15:55 UTC | |
by McDarren (Abbot) on Dec 08, 2005 at 16:11 UTC | |
by ikegami (Patriarch) on Dec 08, 2005 at 16:37 UTC | |
by McDarren (Abbot) on Dec 08, 2005 at 17:13 UTC | |
by McDarren (Abbot) on Dec 09, 2005 at 00:34 UTC | |
by ikegami (Patriarch) on Dec 09, 2005 at 06:18 UTC |