areyouready344
asked on
How to group within a group using Perl's regex?
I have the following line of code if(/^\@([^\n]*).*(^1\.[^\n ]*)/ms) below that needs further grouping for the second group
(^1\.[^\n]*)
#!/usr/bin/perl
use strict;
use warnings;
open FH, '<', 'dd' or die $!;
$/='__Data__';
while(<FH>)
{
if(/^\@([^\n]*).*(^1\.[^\n ]*)/ms)
{
print $1,"\n",$2,"\n\n";
}
}
For example, I have the following data record. I would like to capture the whole number line in which this code does this (^1\.[^\n]*) but I also would like to subcapture the (dkdkdkd-) leading to the hyphen so I can color code it using the html color tag.
__Data__
@scsi_test1
passed
stop
1. dkdkdkd- dldldldldldldldl
My way was to do the following (see below) but this does not work because it also captures the other lines after line number 1.
if(/^\@([^\n]*).*(^1\.(.*: )[^\n]*)/m s)
(^1\.[^\n]*)
#!/usr/bin/perl
use strict;
use warnings;
open FH, '<', 'dd' or die $!;
$/='__Data__';
while(<FH>)
{
if(/^\@([^\n]*).*(^1\.[^\n
{
print $1,"\n",$2,"\n\n";
}
}
For example, I have the following data record. I would like to capture the whole number line in which this code does this (^1\.[^\n]*) but I also would like to subcapture the (dkdkdkd-) leading to the hyphen so I can color code it using the html color tag.
__Data__
@scsi_test1
passed
stop
1. dkdkdkd- dldldldldldldldl
My way was to do the following (see below) but this does not work because it also captures the other lines after line number 1.
if(/^\@([^\n]*).*(^1\.(.*:
You're close...
if(/(^\@[^\n]*).*(^1\.\s*( [^\n-]*)[^ \n]*)/ms)
will do it. The sub-capture (string up to the dash) is $3.
if(/(^\@[^\n]*).*(^1\.\s*(
will do it. The sub-capture (string up to the dash) is $3.
ASKER
sorry for not being clear, but that's not what I really want. I need that line grouped flat as in
(1.) (dkdkdkd-) (dldldldldldldldl)
$1, $2, $3
this will allow me to hightlight $2 using html color tag..
(1.) (dkdkdkd-) (dldldldldldldldl)
$1, $2, $3
this will allow me to hightlight $2 using html color tag..
ASKER
I figured it out... thanks...
if(/^\@([^\n]*).*((^1\.)(\ s*[^\n:]*) ([^\n]*))/ ms)
if(/^\@([^\n]*).*((^1\.)(\
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER