我想做的事情看似简单,但我找不到一种方法来使其发挥作用。
我想匹配一个数字,捕获它,然后匹配字符串中后面的N个字符。天真地,我认为这样的事情会起作用:
$myString = "1abc3cdf\n";
# Capture the number, and use a back-reference in the {} to define how many characters to match
$myString =~ s/(\d+).{\1}//g;
print $myString;
Run Code Online (Sandbox Code Playgroud)
我期望得到bc,但它只返回原始字符串1abc3cdf,即没有发生替换。
我尝试使用扩展的正则表达式(即s///ge),但这没有帮助。有什么建议么?