CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem with easy regex problem

 



PerLLoRe
New User

Jul 14, 2017, 12:13 AM

Post #1 of 3 (601 views)
Problem with easy regex problem Can't Post

I am trying to make sense of Perl regex. So far no good. I cannot solve this problem:

Code
#!/usr/bin/perl 

use 5.18.0;
use warnings;

my $str = "WhatMShallTweMdoT";


I want to get a result like this:
What
Shall
we
do


Laurent_R
Veteran / Moderator

Jul 14, 2017, 7:33 AM

Post #2 of 3 (597 views)
Re: [PerLLoRe] Problem with easy regex problem [In reply to] Can't Post

Maybe you should explain more the logic of your question, but you could do this:


Code
my @words = split /[MT]/, $str;

The @words array not contains:

Code
0  'What' 
1 'Shall'
2 'we'
3 'do'


Or, if you just want to get a string with new lines:

Code
my $str = "WhatMShallTweMdoT"; 
$str =~ s/[MT]/\n/g;


The $str string now has four lines:

Code
What 
Shall
we
do

Is this what you were looking hor?

HTH.


PerLLoRe
New User

Jul 14, 2017, 1:36 PM

Post #3 of 3 (592 views)
Re: [Laurent_R] Problem with easy regex problem [In reply to] Can't Post

Yes, and more. Thank you!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives