Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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

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 perl正则表达式返回数字而不返回字符串_Regex_Perl_Regex Greedy - Fatal编程技术网

Regex perl正则表达式返回数字而不返回字符串

Regex perl正则表达式返回数字而不返回字符串,regex,perl,regex-greedy,Regex,Perl,Regex Greedy,我正在尝试删除字符串中的所有空行。我的字符串是一个有很多随机空行的段落,我正在努力清理它 例如: 应该是 this is an example lots of empty lines in the paragraph 我当前使用的代码只返回随机数字。。好像是在数词什么的 e、 g 或 这就是它的全部回报,没有文字,没有段落 我的代码如下所示 首先假设匹配,然后在匹配后打印所有单词 然后我想删除所有的空行来清理输出 my ($shorten) = $origin =~ /word to ma

我正在尝试删除字符串中的所有空行。我的字符串是一个有很多随机空行的段落,我正在努力清理它

例如:

应该是

this is an example
lots of empty
lines 
in the paragraph
我当前使用的代码只返回随机数字。。好像是在数词什么的

e、 g

这就是它的全部回报,没有文字,没有段落

我的代码如下所示

首先假设匹配,然后在匹配后打印所有单词 然后我想删除所有的空行来清理输出

 my ($shorten) = $origin =~ /word to match\s*(.*)$/s;

 my ($cleanlines) = $shorten =~ s/\n//g;
$shorten部分工作正常,但$cleanlines部分不工作。

此行

my ($cleanlines) = $shorten =~ s/\n//g;
删除
$shorten
中的所有换行符,并存储
$cleanlines

如果要从
$shorten
中删除空行,则必须改为编写此代码

(my $cleanlines = $shorten) =~ s/^\s*\n//gm;
这条线

my ($cleanlines) = $shorten =~ s/\n//g;
删除
$shorten
中的所有换行符,并存储
$cleanlines

如果要从
$shorten
中删除空行,则必须改为编写此代码

(my $cleanlines = $shorten) =~ s/^\s*\n//gm;
my($shorten)=$origin=~/word以匹配\s*(*)$/s
之所以有效,是因为您在正则表达式中使用了捕获括号,而与
(*)
匹配的任何内容都以
$shorten
结束

要从字符串中删除空行,可以使用以下简单的正则表达式:

$shorten =~ s/\n+\n/g;
将对
$shorten
变量执行替换。如果要保持
$shorten
不变,并在新变量中保留已清理的行,只需将
$shorten
的内容复制到新变量中,然后对其执行替换:

my $cleanlines = $shorten;
$cleanlines =~ s/\n+/\n/g;
my($shorten)=$origin=~/word以匹配\s*(*)$/s
之所以有效,是因为您在正则表达式中使用了捕获括号,而与
(*)
匹配的任何内容都以
$shorten
结束

要从字符串中删除空行,可以使用以下简单的正则表达式:

$shorten =~ s/\n+\n/g;
将对
$shorten
变量执行替换。如果要保持
$shorten
不变,并在新变量中保留已清理的行,只需将
$shorten
的内容复制到新变量中,然后对其执行替换:

my $cleanlines = $shorten;
$cleanlines =~ s/\n+/\n/g;

这不会删除除空格或制表符之外的空行,但可能是所有需要的,不会删除除空格或制表符之外的空行,但可能是所有需要的