How to group within a group using Perl's regex?

Posted on 2011-02-21
Last Modified: 2012-06-27
I have the following line of code  if(/^\@([^\n]*).*(^1\.[^\n]*)/ms) below that needs further grouping for the second group


use strict;
use warnings;

open FH, '<', 'dd' or die $!;


                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.

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.

Question by:areyouready344
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Author Comment

ID: 34946341
I"m not getting no output.
LVL 26

Expert Comment

ID: 34946440
You're close...


will do it.  The sub-capture (string up to the dash) is $3.

Author Comment

ID: 34946790
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..

Author Comment

ID: 34946997
I figured it out... thanks...

LVL 26

Accepted Solution

wilcoxon earned 500 total points
ID: 34947399
I think you'll end up with the pieces being $1, $3, $4.  To get them to be $1, $2, $3, and make it work slightly better (very tiny performance increase and put space outside highlight):


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl, group, sort, count question 6 151
Trying to use two loops in a perl script but not getting results need. 10 100
problem with using the glob function 1 77
perl syntax 3 36
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question