[英]escape special character in perl when splitting a string
i have a file in this format 我有这种格式的文件
string: string1
string: string2
string: string3
i want to split the lines by space
and :
,so initially i wrote this: 我想用
space
和:
分割线,所以最初我这样写:
my @array = split(/[:\s]/,$lineOfFile);
the result wasn't as expected, because inside @array
the split
inserts also white space
, so after some researches i understood that i have to escape the \\s
so i wrote 结果不是预期的,因为在
@array
内split
插入内容也留有white space
,因此经过一些研究,我了解到我必须转义\\s
所以我写了
my @array = split(/[:\\s]/,$lineOfFile);
why i have to escape \\s
, the character :
isn't a special character or not? 为什么我必须转义
\\s
字符:
不是特殊字符吗?
can someone explain me that? 有人可以向我解释吗?
thanks in advance. 提前致谢。
You do not need to double escape \\s
and the colon is not a character of special meaning. 您不需要两次转义
\\s
,并且冒号不是特殊含义的字符。 But in your case, it makes sense to avoid using a character class altogether and split on a colon followed by whitespace "one or more" times. 但是,在您的情况下,避免完全使用字符类并在冒号后将空格分隔“一次或多次”是有意义的。
my @array = split(/:\s+/, $lineOfFile);
You don't have to double up the backslash. 您不必加倍反斜杠。 Have you tried it?
你试过了吗?
split /[:\\s]/, $line
will split on a colon :
or a backslash \\
or a small S s
, giving 将分裂上的冒号
:
或反斜杠\\
或小S s
,给
("", "tring", " ", "tring1")
which isn't what you want at all. 这根本不是您想要的。 I suggest you split on a colon followed by zero or more spaces
我建议您在冒号后再分割零个或多个空格
my @fields = split /:\s*/, $line
which gives this result 这给出了这个结果
("string", "string1")
which I think is what you want. 我想这就是你想要的。
The problem is, that /[:\\s]/
only searches for a single character. 问题是,
/[:\\s]/
仅搜索单个字符。 Thus, when applying this regex, you get something like 因此,当应用此正则表达式时,您会得到类似
print $array[0], ' - ', $array[1], ' - ', $array[2];
string - - string1
because it splits between :
and the whitespace before string1
. 因为它在
:
和string1
之前的空白之间分割。 The string string: string1
is therefore splitted into three parts, string
, the empty place between :
and the whitespace and string1
. 因此,字符串
string: string1
分为三部分, string
:
和之间的空白处,以及whitespace和string1
。 However, allowing more characters 但是,允许更多字符
my @array = split(/[:\s]+/,$lineOfFile);
works well, since :
+whitespace is used for splitting. 效果很好,因为
:
+空格用于拆分。
print $array[0], ' - ', $array[1];
string - string1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.