engineersmnky
engineersmnky

Reputation: 29308

Perl Regex Multiple Capture Groups

I am never going to say I am an expert at regex and I am having an issue with this one I am sure due to my lack of understanding. If someone could please try and explain to me how to handle this situation I would really appreciate it.

string = "hello.world with_args, and_more_args #plus a comment

Regular Expression

/^\w*\.(\w+)\s+(.*?)([^#]*)$/

Groups

1. world
2. with_args, and_more_args #
3. plus a comment

The output I am hoping for is

1.world
2.with_args, and_more_args
3.#plus a comment

Any suggestions would be greatly appreciated and if you could teach me something along the way i certainly won't complain.

Upvotes: 2

Views: 4061

Answers (2)

Borodin
Borodin

Reputation: 126722

It is probably best to check for non-hash characters in the second capture; otherwise the pattern will match only lines with a comment.

I suggest this

use strict;
use warnings;

my $s = 'hello.world with_args, and_more_args #plus a comment';

$s =~ / \w*\.(\w+) \s+ ([^#]*) (#.*)? /x;

print "$_\n" for $1, $2, $3;

output

world
with_args, and_more_args 
#plus a comment

Upvotes: 2

anubhava
anubhava

Reputation: 784918

You can use this:

^\w*\.(\w+)\s+(.*?) *(#[^#]*)$

Online Demo

  • To capture # in last group it is important to include # in your last capture group i.e. (#[^#]*).
  • I added * between group # and #3 to avoid capturing trailing space in 2nd group.

Upvotes: 3

Related Questions