Link to home
Start Free TrialLog in
Avatar of areyouready344
areyouready344Flag for United States of America

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]*)/ms)
Avatar of areyouready344
areyouready344
Flag of United States of America image

ASKER

I"m not getting no output.
Avatar of wilcoxon
You're close...

if(/(^\@[^\n]*).*(^1\.\s*([^\n-]*)[^\n]*)/ms)

will do it.  The sub-capture (string up to the dash) is $3.
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..
I figured it out... thanks...

if(/^\@([^\n]*).*((^1\.)(\s*[^\n:]*)([^\n]*))/ms)
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial