0

I have a input txt file containing multiple number of lines in the mentioned format.

JMOD_01 :: This is starting of grouping 2nd KFGJHFG RTIRT DFB SFJKF ERIEFF FJDKF OIOIISD SDJKD 
last line ______________ 5564 numerical digits.

This is second starting of grouping 2nd KFGJHFG RTIRT FSFJKF  
ERIEFF FJDKF OIOIISD SDJKD 
till this end ___________ 021542 some random digits.

I am trying to read this file and extract the searched pattern in a grouping manner

This is below, what I have tried. I tried, grouping the first match and it is getting captured properly. Issue is coming while looking for second grouping as , it is not considering the next line elements.

open(IFH,'<',"file.txt");

while ($line = <IFH>) {
if ($line =~ /^\s*(\w+\_\d*.*)\s*::(.*)/s) {
print "$1\n";
print "$2\n";
}
}
close(IFH);

Expected result :

print $1; #This should give me

JMOD_01
fdgh_6765_546/456

and when , print $2; #then it should give me

"This is starting of grouping 2nd KFGJHFG RTIRT DFB SFJKF ERIEFF FJDKF OIOIISD SDJKD last line"

"This is second starting of grouping 2nd KFGJHFG RTIRT FSFJKF  
ERIEFF FJDKF OIOIISD SDJKD till this end"

and when, print $3; #then it should give

"5564 numerical digits"
"021542 some random digits"

But actual output is coming different for 2nd grouping : print $2; #actual output

"This is first starting of grouping 2nd KFGJHFG RTIRT DFB SFJKF"

"This is second starting of grouping 2nd KFGJHFG RTIRT FSFJKF"
1
  • 1
    Yes,Please ignore that. Consider the below inputs : JMOD_01 :: This is starting of grouping 2nd KFGJHFG RTIRT DFB SFJKF ERIEFF FJDKF OIOIISD SDJKD last line ______________ 5564 numerical digits. fdgh_6765_546/456 :: This is second starting of grouping 2nd KFGJHFG RTIRT FSFJKF ERIEFF FJDKF OIOIISD SDJKD till this end ___________ 021542 some random digits. Thanks for pointing it. Commented Jun 8, 2019 at 18:58

1 Answer 1

1

If I correctly understand the problem, we can likely use two simple expressions and extract our desired data, if that'd be OK:

([A-Z_0-9]+)\s+::\s+([\s\S]+)

Demo 1

Test

use strict;

my $str = 'JMOD_01 :: This is starting of grouping 2nd KFGJHFG RTIRT DFB SFJKF ERIEFF FJDKF OIOIISD SDJKD 
last line ______________ 5564 numerical digits.

This is second starting of grouping 2nd KFGJHFG RTIRT FSFJKF  
ERIEFF FJDKF OIOIISD SDJKD 
till this end ___________ 021542 some random digits.

';
my $regex = qr/([A-Z_0-9]+)\s+::\s+([\s\S]+)/mp;

if ( $str =~ /$regex/g ) {
  print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
  # print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
  # print "Capture Group 2 is $2 ... and so on\n";
}

# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}

and for extracting our digits:

([0-9]+\snumerical digits|[0-9]+\ssome random digits)

Demo 2

Test

use strict;

my $str = 'JMOD_01 :: This is starting of grouping 2nd KFGJHFG RTIRT DFB SFJKF ERIEFF FJDKF OIOIISD SDJKD 
last line ______________ 5564 numerical digits.

This is second starting of grouping 2nd KFGJHFG RTIRT FSFJKF  
ERIEFF FJDKF OIOIISD SDJKD 
till this end ___________ 021542 some random digits.

';
my $regex = qr/([0-9]+\snumerical digits|[0-9]+\ssome random digits)/mp;

if ( $str =~ /$regex/g ) {
  print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
  # print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
  # print "Capture Group 2 is $2 ... and so on\n";
}

# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I combined the mentioned regex in my code and now its taking all the three groupings properly. Thanks Emma.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.