3

Say I have a file that contains lines that look like this:

"7sunrIsEfoo"
"10ecological09"
"3bedtime"

Each line starts with numeral(s) that represent number n. I want to match the n characters following it. The output should be:

sunrIsE
ecological
bed

Is there a way to do this using a regular expression? My first attempt was:

([0-9]*)[a-zA-Z]{\1}

but it doesn't seem to work.

sawa
  • 165,429
  • 45
  • 277
  • 381
qdii
  • 12,505
  • 10
  • 59
  • 116

4 Answers4

4

That's not possible with regex.

([0-9]*) just "remembers" the digits as a substring: they can't be used numerically.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
2

In Ruby, you could use:

result = string[/(\d+)([a-zA-Z]+)/,2][0,$1.to_i]

It will give you the expected result.

sawa
  • 165,429
  • 45
  • 277
  • 381
Aymeric
  • 906
  • 9
  • 20
  • I'm sure that Perl has some way to do this too by mixing in plain programming logic. Just note that it's no regex anymore (in case the OP is unaware of it) – Bart Kiers May 20 '11 at 09:34
  • Totally agree with you. That was a complement of the two previous answers on how to implement it. – Aymeric May 20 '11 at 09:36
0

Regular expressions are not well suited for this task. They have no built it way to interpret numbers. You could use a (messy) workaround by using

(?<=1).|(?<=2)..|(?<=3)...| 

and so on (Though you'd better use the reverse order, otherwise you'll have problems when reaching 11). Note that you should not use this method unless you really really have no other way =)

Jens
  • 25,229
  • 9
  • 75
  • 117
  • 1
    It's like answering someone who asks how to dig an Olympic swimming pool with a teaspoon :) – Bart Kiers May 20 '11 at 09:37
  • 1
    @Bart: Hehe, I agree. When I see these questions, I usually feel challenged to do something stupid with regular expression. And when I've found a solution, I might as well post it. The OP may be in some very obscure situation, where he really can't use anything but regex. And you can dig swimming pools with a teaspoon. You just don't want to. =) – Jens May 20 '11 at 09:43
0

Here is a way to do it in Perl:

#!/usr/local/bin/perl
use strict;
use warnings;

my @list = ("7sunrIsEfoo", "10ecological09", "3bedtime");
foreach(@list) {
    s/^(\d+)(.+)$/substr($2, 0, $1)/e;
    print $_,"\n";
}

output:

sunrIsE
ecological
bed
Toto
  • 89,455
  • 62
  • 89
  • 125