Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Split string in groups with non white space using regex

by karlgoethebier (Abbot)
on Jan 04, 2018 at 12:00 UTC ( [id://1206680]=note: print w/replies, xml ) Need Help??


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

"...I am not really good with regex..."

#MeToo - but didn't you miss a group:

#!/usr/bin/env perl use strict; use warnings; use feature qw(say); my $string = q'Thanos1983+|Thanos1983+|Thanos1983+'; $string =~ m/(.+)\|(.+)\|(.+)/; say for ($1,$2,$3); __END__

Untested. And i hope i didn't miss the point.

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

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 12:12 UTC

    Hello karlgoethebier,

    To be honest I was just about to post another possible solution:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; my @tests = ('Thanos1983+|Thanos1983+', 'Thanos1983+| ', 'Thanos1983+|'); for (@tests) { say "I found:\t\$1: '$1'\t\$2: '$2'\t\$3: '$3'" if /(.*)(\|)(.*)/; } __END__ $ perl test.pl I found: $1: 'Thanos1983+' $2: '|' $3: 'Thanos1983+' I found: $1: 'Thanos1983+' $2: '|' $3: ' ' I found: $1: 'Thanos1983+' $2: '|' $3: ''

    Thanks for the tip of using m/(.+)(\|)(.+)/ this is also works for the first two cases but not for the third.

    Sample of code:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; my @tests = ('Thanos1983+|Thanos1983+', 'Thanos1983+| ', 'Thanos1983+|'); for (@tests) { say "I found:\t\$1: '$1'\t\$2: '$2'\t\$3: '$3'" if /(.+)(\|)(.+)/; } __END__ $ perl test.pl I found: $1: 'Thanos1983+' $2: '|' $3: 'Thanos1983+' I found: $1: 'Thanos1983+' $2: '|' $3: ' '

    Thank you for your time and effort.

    BR / Thanos

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

      (.+)\|? AKA one or none?

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1206680]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-03-29 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found