分割括号中的一个项目的字符串 [英] Splitting a string where one item is in parentheses

查看:217
本文介绍了分割括号中的一个项目的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面找到我的代码.

Please find my code below.

str = "1791 (AR6K Async) S 2 0 0 0 -1 2129984 0 0 0 0 0 113 0 0 20 0 1 0 2370 0 0 4294967295 0 0 0 0 0 0 0 2147483647 0 3221520956 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0"

for val in str:gmatch("%S+") do
  print(val)
end

输出:

1791                                                                          
(AR6K                                 
Async)                  
S           
2            
0                 
0           
0            
-1
....

但是我希望输出像

1791
(AR6K Async)
S
2
0 
0 
0
-1
...

任何人都可以帮助我如何将括号中的值作为单个值而不是单独的值获取.

Can anyone please help me how to get the values in bracket as a single value instead getting separate values.

推荐答案

str = "1791 (AR6K Async) S 2 0 0 0 -1 2129984 0 0 0 0 0 113 0 0 20 0 1 0 2370 0 0 4294967295 0 0 0 0 0 0 0 2147483647 0 3221520956 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0"

for val in str:gsub("%S+","\0%0\0")
              :gsub("%b()", function(s) return s:gsub("%z","") end)
              :gmatch("%z(.-)%z") do
   print(val)
end

说明:
1.用零标记"将所有无空格子字符串括起来
(在开头添加一个二进制零字符,在末尾添加一个)
2.删除括号内的零标记"
3.显示所有周围的零件

Explanation:
1. Surround all spaceless substrings with "zero marks"
(add one binary-zero-character at the beginning and one at the end)
2. Remove "zero marks" from inside parentheses
3. Display all surrounded parts

这篇关于分割括号中的一个项目的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆