in reply to Split string in groups with non white space using regex

#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1206677 use strict; use warnings; for ( 'Thanos1983+|Thanos1983', 'Thanos1983+|' ) { print; /([^|]*)(\|)([^|]*)/ and print "I found:\t\$1: '$1'\t\$2: '$2'\t\$3: + '$3'"; }

Outputs:

Thanos1983+|Thanos1983 I found: $1: 'Thanos1983+' $2: '|' $3: 'Thanos1983' Thanos1983+| I found: $1: 'Thanos1983+' $2: '|' $3: ''

Replies are listed 'Best First'.
Re^2: Split string in groups with non white space using regex
by thanos1983 (Parson) on Jan 04, 2018 at 11:53 UTC

    Hello tybalt89,

    Works perfectly, thanks for your time and effort.

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Note that the string  '([^|]*)(\|)([^|]*)' also successfully parses  '|'  '||'  '|||'

      c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(pp); ;; my $rx_string = '([^|]*)(\|)([^|]*)' ; ;; for my $s ( 'Thanos1983+|', 'Thanos1983+| ', 'Thanos1983+| ', 'Thanos1983+|Thanos1983+', '|Thanos1983+', ' |Thanos1983+', ' |Thanos1983+', '+++|+++', '|', '||', '|||', ) { my $parsed = my @captured = $s =~ $rx_string; if ($parsed) { print qq{'$s' -> }, pp \@captured; } else { print qq{failed to parse '$s'}; } } " 'Thanos1983+|' -> ["Thanos1983+", "|", ""] 'Thanos1983+| ' -> ["Thanos1983+", "|", " "] 'Thanos1983+| ' -> ["Thanos1983+", "|", " "] 'Thanos1983+|Thanos1983+' -> ["Thanos1983+", "|", "Thanos1983+"] '|Thanos1983+' -> ["", "|", "Thanos1983+"] ' |Thanos1983+' -> [" ", "|", "Thanos1983+"] ' |Thanos1983+' -> [" ", "|", "Thanos1983+"] '+++|+++' -> ["+++", "|", "+++"] '|' -> ["", "|", ""] '||' -> ["", "|", ""] '|||' -> ["", "|", ""]


      Give a man a fish:  <%-{-{-{-<

        Hello AnomalousMonk,

        Thanks for the test script I had no idea that the regex can match so many combinations.

        BR / Thanos

        Seeking for Perl wisdom...on the process of learning...not there...yet!