如何匹配空行
我正在尝试根据空行拆分变量。变量是
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没什么可做的:
或者,您可以使用字符串 iSCSI 的正向前瞻来分割多个空白字符:
这适用于您的简单情况,无论空白字符是换行符、回车符还是空格。
There's nothing to it:
Alternatively you can split on multiple whitespace characters with positive lookahead for the string
iSCSI
:That works for your simple case, no matter if the whitespace characters are line feeds, carriage returns or spaces.
关于如何处理您拥有的字符串有很多好的答案。但让我们猜测一下您应该问哪个问题。
如何按段落读取文件? - 这恰好是 常见问题。
如果您从文件中读取这些数据,那么您最好在读入时正确解析它。
当然,这只是对您想要执行的操作的猜测。如果我离题太远了,请忽略我。
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.
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.
如果“空”行包含一些空格,您可以执行以下操作:
或者,与 unicode 兼容:
If "empty" lines contain somes spaces, you can do:
or, to be unicode compatible:
使用 split 以 '\n\s' 作为分隔符,
Use split with '\n\s' as separator,