大家好我对perl很新。我有一个像$string = "hi\n how are \t you?";
字符串这样的字符串本身包含"\n"
和"\t"
。我怎么用什么都替换它们?
代表:
verfText = "Verify the following in ESIS-5000 display:\n"
verfText += " - The Air Data Source submenu is displayed as following from top to bottom:\n"
verfText += "\ta)Submenu Title\n\tb)Standby Source\n\tc)ADS Source\n\td)Back Button\n\n"
verfText += " - The air data source submenu title with the "
verfText += "text 'ADS' on line one and 'Source' on line two.\n\n"
verfText += " - Standby Source(ADS4) validity is valid and the Standby Source menu item is shown as a radio button "
verfText += "with the label 'STBY' on line one and 'ADS' on line two.\n\n"
verfText += " - Label 'ADS' on line one and 'Source' on line two in ADS Source momentary submenu button and "
verfText += "it is displayed in white."
我想替换字符串中的所有"\n"
和"\t"
,请帮帮我。
答案 0 :(得分:2)
您可以使用tr
运算符
$string =~ tr|\n\t||d;
解释
/d
修饰符删除已找到但未替换的字符。
另一方面,如果你想替换
中的文字\n
和\t
$string = 'hi\n how are \t you?';
然后你可以使用正则表达式,
$string =~ s|\\[nt]||g;
答案 1 :(得分:1)
您的代码看起来不像perl。没有分号,你使用+ =,这是数字而不是。=这是字符串。看起来应该更像这样:
#!/usr/bin/perl
use strict;
use warnings;
my $verfText;
$verfText = "Verify the following in ESIS-5000 display:\n" ;
$verfText .= " - The Air Data Source submenu is displayed as following from top to bottom:\n";
$verfText .= "\ta)Submenu Title\n\tb)Standby Source\n\tc)ADS Source\n\td)Back Button\n\n" ;
$verfText .= " - The air data source submenu title with the " ;
$verfText .= "text 'ADS' on line one and 'Source' on line two.\n\n" ;
$verfText .= " - Standby Source(ADS4) validity is valid and the Standby Source menu item is shown as a radio button " ;
$verfText .= "with the label 'STBY' on line one and 'ADS' on line two.\n\n" ;
$verfText .= " - Label 'ADS' on line one and 'Source' on line two in ADS Source momentary submenu button and " ;
$verfText .= "it is displayed in white." ;
#print $verfText;
$verfText =~ s/[\t\n]//g;
print $verfText;
答案 2 :(得分:0)
我希望这对你有用..尝试一次。
"hi\nhow are \t you?".replace(/[\n|\t]/g, "");
如果没有告诉我什么是错的。好的
所以你使用的是verfText变量,最后写下这一行
verfText = verfText.replace(/[\n|\t]/g, "");