如何用空格和双引号分割字符串

时间:2022-08-22 12:50:35

I have a input of string with spaces and double quotes as below:

我有一个带空格和双引号的字符串输入如下:

Input :

输入:

18 17 16 "Arc 10 12 11 13" "Segment 10 23 33 32 12" 23 76 21

Expected Output:

预期的输出:

18
17
16
Arc 10 12 11 13
Segment 10 23 33 32 12 
23
76
21

How can I do this using Regex? Thank you in advance

如何使用Regex进行此操作?提前谢谢你

2 个解决方案

#1


3  

You can use next regexp(see example):

您可以使用next regexp(参见示例):

("[^"]+")|\S+

(“[^]+)| \ S +

  • ("[^"]+") - quoted sequence.
  • (“[^]+)——引用序列。
  • \S+ - non whitespace sequence.
  • \S+ -非空格序列。

Probably order of groups is depend from regexp implementation. In the demo engine matching stared from left to right. Also do not forget escape special characters with double slash.

组的顺序可能取决于regexp实现。在演示引擎匹配中,从左到右盯着。也不要忘记用双斜杠转义特殊字符。

#2


0  

"(.+?)"|(\w+(?=\s|$))

“|”(. + ?)(\ w)+(? = \ s | $))

check here

检查在这里

#1


3  

You can use next regexp(see example):

您可以使用next regexp(参见示例):

("[^"]+")|\S+

(“[^]+)| \ S +

  • ("[^"]+") - quoted sequence.
  • (“[^]+)——引用序列。
  • \S+ - non whitespace sequence.
  • \S+ -非空格序列。

Probably order of groups is depend from regexp implementation. In the demo engine matching stared from left to right. Also do not forget escape special characters with double slash.

组的顺序可能取决于regexp实现。在演示引擎匹配中,从左到右盯着。也不要忘记用双斜杠转义特殊字符。

#2


0  

"(.+?)"|(\w+(?=\s|$))

“|”(. + ?)(\ w)+(? = \ s | $))

check here

检查在这里