Pergunta

Example data:

029Extract this specific string. Do not capture anything else.

In the example above, I would like to capture the first n characters immediately after the 3 digit entry which defines the value of n. I.E. the 29 characters "Extract this specific string."

I can do this within a loop, but it is slow. I would like (if it is possible) to achieve this with a single regex statement instead, using some kind of backreference. Something like:

(\d{3})(.{\1})
Foi útil?

Solução

With perl, you can do:

my $str = '029Extract this specific string. Do not capture anything else.';
$str =~ s/^(\d+)(.*)$/substr($2,0,$1)/e;
say $str;

output:

Extract this specific string.

Outras dicas

You can not do it with single regex, while you can use knowledge where regex stop processing to use substr. For example in JavaScript you can do something like this http://jsfiddle.net/75Tm5/

var input = "blahblah 011I want this, and 029Extract this specific string. Do not capture anything else.";
var regex = /(\d{3})/g;
var matches;
while ((matches = regex.exec(input)) != null) {
    alert(input.substr(regex.lastIndex, matches[0]));
}

This will returns both lines:

I want this
Extract this specific string.

Depending on what you really want, you can modify Regex to match only numbers starting from line beginning, match only first match etc

Are you sure you need a regex?

From https://stackoverflow.com/tags/regex/info:

Fools Rush in Where Angels Fear to Tread

The tremendous power and expressivity of modern regular expressions can seduce the gullible — or the foolhardy — into trying to use regular expressions on every string-related task they come across. This is a bad idea in general, ...

Here's a Python three-liner:

foo = "029Extract this specific string. Do not capture anything else."
substr_len = int(foo[:3])
print foo[3:substr_len+3]

And here's a PHP three-liner:

$foo = "029Extract this specific string. Do not capture anything else.";
$substr_len = (int) substr($foo,0,3);
echo substr($foo,3,substr_len+3);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top