Search code examples
regexcharacterdigits

Regular expression matching a number of characters which amount had been parsed before


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.


Solution

  • That's not possible with regex.

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