如何匹配空行

发布于 2024-12-20 23:24:29 字数 222 浏览 0 评论 0原文

我正在尝试根据空行拆分变量。变量是

$s = "iSCSI cred number=0
name=match1
string=2
name=number1

iSCSI cred number=1
name=match2
string=3
name=number2

iSCSI cred number=2
name=match3
string=4
name=number3";

I am trying to split the variable based on the blank line. the variable is

$s = "iSCSI cred number=0
name=match1
string=2
name=number1

iSCSI cred number=1
name=match2
string=3
name=number2

iSCSI cred number=2
name=match3
string=4
name=number3";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

冷了相思 2024-12-27 23:24:29

没什么可做的:

my @split_values = split/\n\n/, $s

或者,您可以使用字符串 iSCSI 的正向前瞻来分割多个空白字符:

my @split_values = split/\s+(?=iSCSI)/, $s;

这适用于您的简单情况,无论空白字符是换行符、回车符还是空格。

There's nothing to it:

my @split_values = split/\n\n/, $s

Alternatively you can split on multiple whitespace characters with positive lookahead for the string iSCSI:

my @split_values = split/\s+(?=iSCSI)/, $s;

That works for your simple case, no matter if the whitespace characters are line feeds, carriage returns or spaces.

泅人 2024-12-27 23:24:29

关于如何处理您拥有的字符串有很多好的答案。但让我们猜测一下您应该问哪个问题。

如何按段落读取文件? - 这恰好是 常见问题

如果您从文件中读取这些数据,那么您最好在读入时正确解析它。

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

local $/ = '';

while (<DATA>) {
  say "Record number $. is:\n$_";
}

__DATA__
iSCSI cred number=0
name=match1
string=2
name=number1

iSCSI cred number=1
name=match2
string=3
name=number2

iSCSI cred number=2
name=match3
string=4
name=number3

当然,这只是对您想要执行的操作的猜测。如果我离题太远了,请忽略我。

Plenty of good answers about how to deal with the string that you have. But let's take a punt at guessing which question you should be asking.

How can I read in a file by paragraphs? - which just happens to be a Frequently Asked Question.

If you're reading this data in from a file, then you'd be better off parsing it correctly as you read it in.

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

local $/ = '';

while (<DATA>) {
  say "Record number $. is:\n$_";
}

__DATA__
iSCSI cred number=0
name=match1
string=2
name=number1

iSCSI cred number=1
name=match2
string=3
name=number2

iSCSI cred number=2
name=match3
string=4
name=number3

This is, of course, just a guess at what you're trying to do. If I'm way off the mark then please ignore me.

忆伤 2024-12-27 23:24:29

如果“空”行包含一些空格,您可以执行以下操作:

split/\n+\s*\n+/, $s;

或者,与 unicode 兼容:

split/\R+\s*\R+/, $s;

If "empty" lines contain somes spaces, you can do:

split/\n+\s*\n+/, $s;

or, to be unicode compatible:

split/\R+\s*\R+/, $s;
他不在意 2024-12-27 23:24:29

使用 split 以 '\n\s' 作为分隔符,

my  @arr =  split('\n\s',$s);

Use split with '\n\s' as separator,

my  @arr =  split('\n\s',$s);
奶茶白久 2024-12-27 23:24:29
my @arr = split(/[\n]{2,}/,$s);
my @arr = split(/[\n]{2,}/,$s);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文