Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-21
5
Medium Priority
?
414 Views
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
(^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)
0
Comment
Question by:areyouready344
[X]
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
5 Comments
 

Author Comment

by:areyouready344
ID: 34946341
I"m not getting no output.
0
 
LVL 27

Expert Comment

by:wilcoxon
ID: 34946440
You're close...

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

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

Author Comment

by:areyouready344
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..
0
 

Author Comment

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

if(/^\@([^\n]*).*((^1\.)(\s*[^\n:]*)([^\n]*))/ms)
0
 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 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):

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

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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 …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

604 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