php regex - 分割字符串 - 一种输出形式
如果输入字符串可以采用以下形式,我想知道用 |
分隔符分割字符串的好方法:
"foo, bar"
"foo ,bar"
"foo,bar"
"foo , bar"
"foo bar"
"foo,,bar"
因此唯一可能的输出字符串如下:
"foo|bar"
"foo|bar|other|here"
与输入中有多少项无关细绳。
任何想法将不胜感激!
I'm wondering a good way of splitting strings by a |
delimiter if the input strings could be of the following form:
"foo, bar"
"foo ,bar"
"foo,bar"
"foo , bar"
"foo bar"
"foo,,bar"
So the only possible outputs strings are as:
"foo|bar"
"foo|bar|other|here"
Independently of how many terms are within the input string.
Any thoughts would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将处理带有逗号的情况;)如果您也需要只有空格的情况:
This will handle the cases with the comma ;) If you need the one with only the whitespace too:
我会这样做:
I would do:
像这样的东西应该可以解决问题......
解释一下:
\b
是一个单词边界,因此它正在寻找两个单词边界之间的空格、逗号或管道的任意组合。Something like this should do the trick...
to explain:
\b
is a word-boundary, so it is looking for any combination of spaces, commas or pipes between two word boundaries.