From: Dr.Ruud on
Mr P wrote:
> On Jul 14, 2:32 pm, Jozxyqk <jfeue...(a)eecs.tufts.edu> wrote:
>> Mr P <misterp...(a)gmail.com> wrote:

>>> Lets say I want the CENTER 2 characters from any (even length) string?
>>> 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.
>>
>> Why use a regex?
>>
>> substr($string,length($string)/2-1,2);
>
> I'd thought of this approach and even played with it a bit- you left
> out the test for *evenness* however. I just love regexes I'd hoped
> there was something simple Id missed like some assertion that would
> make it EZ.. Ben's solution is pretty durn good though!

Question 1 was: Why use a regex?
Question 2 is: Why did you lie about the string having an even length?

And as a direction, you presented a substitution in stead of a regex.

perl -wle '
my $center = @ARGV ? $ARGV[0] : q{TE$T};
exit 1 if $center =~ /\A(?:..)*.\z/s; # odd

1 while $center =~ s/\A.(.{2,}).\z/$1/s;

print $center;
'
E$

--
Ruud
From: Justin C on
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..

Don't use a regex, use substr() and some maths.

Something like: my $str = substr($_, (length($_)/2) - 1, 2);

I see that you are asking for a regex, and while I don't think that's
the appropriate way to do it, it is possible that you're asking for a
regex to do this for a reason. If you really want a regex then that's
beyond my current regex ability.... actually:

my $c = length($_);
$_ =~ s/^.{$c}(.{2}).{$c}/$1/;

Thank you for the little puzzle which has aided my education!

Justin.
From: Justin C on
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 know my last post was wrong! I'm just posting before everyone jumps on
it! What I meant was:

my $c = (length($_) / 2) - 1;
$_ =~ s/^.{$c}(.{2}).{$c}$/$1/;

Justin.
From: Dr.Ruud on
Justin C wrote:

> my $c = (length($_) / 2) - 1;
> $_ =~ s/^.{$c}(.{2}).{$c}$/$1/;

You can get into trouble there with embedded newlines.

You can change it to

( $_ ) = /^ .{$c} (..) /sx;

Or even better, to

$_ = substr( $_, length() / 2 - 1, 2 );

--
Ruud
From: Mr P on
Just to see what error it would throw- I tried this:

s/^(.*)(..)(.*)$/$2/ if length $1 == length $2;

Curiously it said *attempted to modified a read-only value* which I
thought was odd- I don't see where I'm trying to do that at all. I
expected it to say something was un-innited or something like that?