Prev: FAQ 9.10 How do I decode or create those %-encodings on the web?
Next: FAQ 3.3 Is there a Perl shell?
From: Justin C on 20 Jul 2010 10:47 On 2010-07-14, Mr P <misterperl(a)gmail.com> wrote: > Lets say I want the CENTER 2 characters from any (even length) string? > > 123456 .. 34 > ABC123 .. C1 > 1223 ..22 > 1234567890 .. 56 > AB .. AB > > > is there A regex that can handle this for ANY even length string? Im > pretty good with regexes but I cant think of one that can handle it. > > sorta like > s/(.+)(..)(.+)/$2/ where the length of $1 == length of $2.. > > Thanks! I'm sure I posted a reply to this already, but it hasn't shown up here after several days. So here it is again: #!/usr/bin/perl use warnings; use strict; while (<DATA>) { chomp; my $n = (length($_) / 2) - 1; $_ =~ /^.{$n}(.{2}).{$n}$/; print $1, "\n"; } __DATA__ bear wooden coldbeer golden cat wobbly stool with three legs Justin. -- Justin C, by the sea.
First
|
Prev
|
Pages: 1 2 3 4 Prev: FAQ 9.10 How do I decode or create those %-encodings on the web? Next: FAQ 3.3 Is there a Perl shell? |