Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 无法识别正则表达式中的空格字符_Regex_Perl - Fatal编程技术网

Regex 无法识别正则表达式中的空格字符

Regex 无法识别正则表达式中的空格字符,regex,perl,Regex,Perl,我正在写一个简单的程序-请看下面我的代码和注释。有人知道为什么第10行不能识别空格字符吗?当我运行代码时,它会找到::,但不会用空格替换它 1 #!/usr/bin/perl 2 3 # This program replaces :: with a space 4 # but ignores a single : 5 6 $string = 'this::is::a:string'; 7 8 print "Current: $string\n"; 9 10 $string =~ s

我正在写一个简单的程序-请看下面我的代码和注释。有人知道为什么第10行不能识别空格字符吗?当我运行代码时,它会找到::,但不会用空格替换它

1  #!/usr/bin/perl
2
3  # This program replaces :: with a space
4  # but ignores a single :
5
6  $string = 'this::is::a:string';
7
8  print "Current: $string\n";
9 
10 $string =~ s/::/\s/g;
11 print "New: $string\n";

使用
s/:://g
\s
仅表示匹配侧的空白,在替换侧它变为
s

尝试
s/:///g
而不是
s/://\s/g


\s
实际上是一个表示所有空白字符的字符类,因此只有在正则表达式(第一部分)中,而不是在替换字符串中才有意义。

替换字符串应该是一个文本空间,即:

$string =~ s/::/ /g;

\s
替换为实空格

\s
是空白匹配模式的缩写。指定替换字符串时不使用它