note
ihb
<p>
If you use <code>.*</code> as "everything else", then be sure to use the <code>/s</code> modifier.
<code> s/^.*?(\d+).*$/$1/s
</code>
Beware of that if there is no digit in <code>$file</code>, then <code>$sk</code> won't change! This is <i>bad</i>. It would look like you need to change <code>\d+</code> to <code>\d*</code>, but that would make it match "" at the beginning, and so <code>$1</code> would be empty, thus erasing the whole string.
</p><p>
But this problem can be solved. By rewriting the pattern to a more natural (?) pattern we'll soon see the solution. First, your pattern can be rewritten to
<code> s/\D*(\d+).*/$1/s
</code>
The anchors are removed, as they're unnecessary. They're unnecessary in your pattern too. Anyhow, now we can change <code>\d+</code> to <code>\d*</code>, and <code>$sk</code> will be empty if no number was matched. So the result is
<code> (my $sk = $file) =~ s/\D*(\d*).*/$1/s;
</code>
But this still isn't fully analogous with
<code> my ($sk) = $file =~ /(\d+)/;
</code>
since <code>$sk</code> will be the empty string in the former, and the undefined value in the latter. So in extraction situations I often stay away from that trick, and simply use the latter.
</p>
<code>ihb</code>
231763
231848