Regex to match only innermost delimited sequence

后端 未结 3 1491
感动是毒
感动是毒 2021-01-14 03:16

I have a string that contains sequences delimited by multiple characters: << and >>. I need a regular expression to only give me the

相关标签:
3条回答
  • 2021-01-14 03:51
    $string = 'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>';
    @matches = $string =~ /(<<(?:[^<>]+|<(?!<)|>(?!>))*>>)/g;
    
    0 讨论(0)
  • 2021-01-14 04:00

    Here's a way to use split for the job:

    my $str = 'do not match this <<but match this>> not this <<BUT NOT THIS <<this too>> IT HAS CHILDREN>> <<and <also> this>>';
    my @a = split /(?=<<)/, $str;
    @a = map { split /(?<=>>)/, $_ } @a;
    
    my @match = grep { /^<<.*?>>$/ } @a;
    

    Keeps the tags in there, if you want them removed, just do:

    @match = map { s/^<<//; s/>>$//; $_ } @match;
    
    0 讨论(0)
  • 2021-01-14 04:10
    @matches = $string =~ /(<<(?:(?!<<|>>).)*>>)/g;
    

    (?:(?!PAT).)* is to patterns as [^CHAR]* is to characters.

    0 讨论(0)
提交回复
热议问题