I'm having problems matching a variable length
string in perl using m// and parens.
I still want perl to be greedy, I just want it
to pick up every char along the way. The code
below just gives me the last char, but I would
like to get all of them between each letter.
Also, if there are any 2 consecutive letters
without digits between them, I want the match
to give me the empty string.
Anyone know what I should change my regex to?
Thanks for your help!
Code:
#!/usr/bin/perl
# in first 2 examples would like to get the entire strings
# between each of the letters
$str= 'a1b22c333d4444e55555f';
$str =~ m/[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*/g;
print "1:$1 2:$2 3:$3 4:$4 \n\n";
#desired output is '1:1 2:22 3:333 4:4444 ....'
$str= 'a1b23c456d7890e12345f';
$str =~ m/[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*/g;
print "1:$1 2:$2 3:$3 4:$4 \n\n";
#desired output is '1:1 2:23 3:456 4:7890 ....'
# also, would like to return empty string if
# any 2 letters have no separation ('abc' in this example)
$str= 'abc456d7890e12345f';
$str =~ m/[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*[a-f]([^a-f])*/g;
print "1:$1 2:$2 3:$3 4:$4 \n\n";
#desired output is '1: 2: 3:456 4:7890 ....'
Here is the output I get:
Code:
1:1 2:2 3:3 4:4
1:1 2:3 3:6 4:0
1: 2: 3:6 4:0
Here is what I want to get:
Code:
1:1 2:22 3:333 4:4444
1:1 2:23 3:456 4:7890
1: 2: 3:456 4:7890