正则表达式按空格分割,保留大括号中的字符串
我有一个看起来像这样的字符串
arg1 {0 1} arg2 {5 87} string {with space} ar3 1
它被空格分割,但字符串也可能包含空格,因此会导致问题对于带有空格的字符串。我仍然需要分割这个字符串,但我不想分割包含在大括号中并以 string
关键字为前缀的字符串。这意味着上面的字符串应该像这样分割
arg1
{0
1}
arg2
{5
87}
string
{with space}
ar3
1
不能实现这个,我真的需要阅读很多关于正则表达式的内容。请你帮助我好吗?
I have a string that looks like that
arg1 {0 1} arg2 {5 87} string {with space} ar3 1
It is split by space, but string may contain spaces as well, so it causes problems for strings with spaces. I still need to split this string, but I'd like to do not split string contained in curl braces and prefixed by string
keyword. That means that the string above should be split like that
arg1
{0
1}
arg2
{5
87}
string
{with space}
ar3
1
Can't implement this, I really need to read a lot about regular expressions. Could you please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
步骤1:照常用空格分割,得到一个数组
步骤2:遍历数组,如果找到
{[a-zA-Z]+
,则用空格连接下一个元素,并删除下一个元素。然后你就得到了你想要的。以下 awk 命令作为示例显示。
==更新==
好的,根据您的评论,这也有效:
步骤1,找出您不想“拆分”的那些字符串,用特殊字符串替换。重要的是将找到的字符串保存到另一个数组中。 grep 示例中的模式:
步骤2,拆分
步骤3,用正确的索引替换特殊字符串。
step 1:split with space as usual, get an array
step 2: go through the array, if find
{[a-zA-Z]+
, join the next element with a space, and remove the next element.then you got what you want. the following awk command shows as an example.
==update==
OK, based on your comment, this works too:
step1, find out those strings that you don't want to "split", replace with a special string. and important is saving found strings to another array. The pattern in grep example:
step2, do split
step3, replace the special string back with right index.
我不了解 QRegExp,所以我不知道它是否具有 lookaround 功能。如果是这样,您可以尝试按如下方式进行拆分:
应该在任何空白字符上进行拆分,除了紧接在单词
string
之前的一对大括号内的空白字符。如果string
关键字已经位于一组大括号内,它将忽略它。您还可以使用简化版本:
(?,尽管这会受到诸如
foo {string { 之类的奇怪内容的影响酒吧qux}}
。I don't know QRegExp, so I don't know if it has lookaround capabilities. If it does, you could try splitting on something like this:
That should split on any whitespace character except those inside a pair of braces immediately preceded by the word
string
. It will ignore thestring
keyword if it's already inside a set of braces.You can also use a simplified version:
(?<!\bstring\s{[^}]*)\s
, although this will be affected by weird stuff likefoo {string {bar qux}}
.