Re: regex assistance
by toolic (Bishop) on Oct 20, 2010 at 12:51 UTC
|
use strict;
use warnings;
use Data::Dumper;
my @array=('GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : tsm
+c11_wld(sxfatd12j)) GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TSPA
+N04] : tspan04-dfdint-wld(sxfamd6f)) POS_WLI02 - POS_WLI02 Web Logic
+Integrator');
my @newarray;
while ($array[0] =~ /(\w+) - /g) {
push @newarray, $1;
}
print Dumper(\@newarray);
__END__
$VAR1 = [
'GAP_SPAN09',
'GAP_SPAN03',
'POS_WLI02'
];
| [reply] [d/l] |
Re: regex assistance
by johngg (Canon) on Oct 20, 2010 at 13:16 UTC
|
knoppix@Microknoppix:~$ perl -Mstrict -wE '
> my @array = (
> q{GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : tsmc11_w
+ld(sxfatd12j))},
> q{GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TSPAN04] : tspan04
+-dfdint-wld(sxfamd6f))},
> q{OS_WLI02 - POS_WLI02 Web Logic Integrator},
> );
> my @newArray =
> map m{^(.*?)\s-\s},
> @array;
> say for @newArray;'
GAP_SPAN09
GAP_SPAN03
OS_WLI02
knoppix@Microknoppix:~$
I hope this is helpful.
| [reply] [d/l] |
Re: regex assistance
by hbm (Hermit) on Oct 20, 2010 at 12:50 UTC
|
You realize @array has only one element, the long single-quoted string?
Perhaps this:
use strict;
my @array=('GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : tsm
+c11
+_wld(sxfatd12j)) GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TSPAN04
+] : tspan04-dfdint-wld(sxfamd6f)) POS_WLI02 - POS_WLI02 Web Logic Int
+egrator');
my @newarray = $array[0] =~ /(\S+) - /g;
print "$_\n" for @newarray;
| [reply] [d/l] |
|
|
Apologies the example given should have looked like this
my @array=('GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : tsm
+c11_wld(sxfatd12j))','GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TS
+PAN04] : tspan04-dfdint-wld(sxfamd6f))','POS_WLI02 - POS_WLI02 Web Lo
+gic Int
+egrator');
So there are 3 elements. This was purely an example though since there could be upto 20 elements in the original array
| [reply] [d/l] |
|
|
Ahh, the question becomes clearer though the logic is largely similar
use strict;
use warnings;
my @records=('GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : t
+smc11_wld(sxfatd12j))',
'GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TSPAN04] : tspan04-d
+fdint-wld(sxfamd6f))',
'POS_WLI02 - POS_WLI02 Web Logic Integrator)');
my @record_ids;
for my $record (@records){
$record=~m/^(\S+)(?= - )/g;
push @record_ids , $1;
}
for my $record_id (@record_ids){
print "$record_id\n";
}
__END__
GAP_SPAN09
GAP_SPAN03
POS_WLI02
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
| [reply] [d/l] [select] |
|
|
my @newarray = map /^(\S+) +- /, @array;
| [reply] [d/l] |
Re: regex assistance
by CountZero (Bishop) on Oct 20, 2010 at 15:27 UTC
|
Or using a split rather than a regexp: use strict;
use warnings;
use 5.012;
my @array = (
'GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : tsmc11_wld(
+sxfatd12j))',
'GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TSPAN04] : tspan04-df
+dint-wld(sxfamd6f))',
'OS_WLI02 - POS_WLI02 Web Logic Integrator',
);
my @results = map{(split / - /)[0]} @array;
say for @results;
Output:GAP_SPAN09
GAP_SPAN03
OS_WLI02
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
|
|
Thanks for all the responses. It just goes to show how versatile Perl is !
| [reply] |
Re: regex assistance
by locked_user sundialsvc4 (Abbot) on Oct 20, 2010 at 13:24 UTC
|
| |
Re: regex assistance
by Utilitarian (Vicar) on Oct 20, 2010 at 12:59 UTC
|
you have a string that looks like $string='GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : tsmc11_wld(sxfatd12j)) GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TSPAN04] : tspan04-dfdint-wld(sxfamd6f)) POS_WLI02 - POS_WLI02 Web Logic Integrator';
Thus @array=$string=~/PATTERN/g is the syntax you need.
The PATTERN is to capture a series of non space characters if they are followed by the ' - ' pattern.
If you need more help than that:
~/$ perl -e '$string=q(GAP_SPAN09 - GAP SPAN base (Scratch Testing [TS
+MC11] : tsmc11_wld(sxfatd12j)) GAP_SPAN03 - GAP SPAN base (DFD E2E Te
+sting [TSPAN04] : tspan04-dfdint-wld(sxfamd6f)) POS_WLI02 - POS_WLI02
+ Web Logic Integrator);@array=$string=~m/(\S+)(?= - )/g;for $record_t
+itle (@array){print "$record_title\n";}'
GAP_SPAN09
GAP_SPAN03
POS_WLI02
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
| [reply] [d/l] [select] |
Re: regex assistance
by umasuresh (Hermit) on Oct 20, 2010 at 13:00 UTC
|
Please show what you have tried so far:
use strict;
use warnings;
my $string ="GAP_SPAN09 - GAP SPAN base (Scratch Testing [TSMC11] : ts
+mc11_wld(sxfatd12j)) GAP_SPAN03 - GAP SPAN base (DFD E2E Testing [TSP
+AN04: tspan04-dfdint-wld(sxfamd6f)) POS_WLI02 - POS_WLI02 Web Logic I
+ntegrator";
my @array;
while ($string =~ m/(\w+\s?)-/g)
{
push (@array, $1);
}
print join("\t", @array);
Your array has only one element. I think meant a string as input!
Must have book for Regex challenges:
Mastering Regular Expressions
UPDATE: Other monks have already posted answers while I was drafting this! | [reply] [d/l] |
Re: regex assistance
by raybies (Chaplain) on Oct 20, 2010 at 14:26 UTC
|
my first instinct was to split the longer string into an array on ' - ' and then use a regex on each element in the array.
foreach (@array) {
@pieces = split / - /;
foreach (@pieces) {
#then just grab the last full word
# do regex stuff here.
if (/(\w+)$/) {
push @newarray, $1;
}
}
}
| [reply] [d/l] |