简体   繁体   中英

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.

You can use this:

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

Online Demo

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM