2013-12-17 66 views
0

我需要写一个perl脚本删除之间的所有特殊字符和空格:和第一字母删除所有特殊字符之间:和第一字母

$53:$? Abc 

应该一代产量

$53:Abc 

任何人可以帮助我出去了吗?

+3

付出了一些努力,尝试一些东西,在这里发布,然后我们会帮你纠正它。 –

+1

什么是“特殊字符”? – ysth

回答

1

使用向后看/提前开始/结束之间的勉强量词匹配:

$var =~ s/(?<=:).*?(?=[a-zA-Z])// 
0

你可以尝试这样的事情。假设$str = "$53:$? Abc";$pattern具有正确的模式,将返回冒号和字母之间的所有内容。

my $newStr = ""; 
if($str =~ $pattern) 
{ 
    $newStr = $`; //add the text to the left of the match 
    //loop through each character of $& (what the regex matched) and remove it 
    //from the string if it is a special character then do the below 
    $newStr += $&; //add the matching text without the special characters 
    $newStr += $'; //add the text to the right of the match 
} 

我在正则表达式相当新的,所以我不能想出正确的匹配模式,但我想至少应该帮助您开始使用,因为它看起来像你甚至没有一个出发点。然而,使用正则表达式可能会有一种“更容易/更好”的方式,但这是我如何解决这个问题的方法。