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
  • 3
  • 2

Author Comment

Comment Utility
I"m not getting no output.
LVL 26

Expert Comment

Comment Utility
You're close...


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

Author Comment

Comment Utility
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

Comment Utility
I figured it out... thanks...

LVL 26

Accepted Solution

wilcoxon earned 500 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now