我有一个表示表中行的字符串,如下所示:
{failures=4, successes=6, name=this_is_a_name, p=40.00}
我制作了一个可与Pattern.split()
一起使用的表达式,以便让我恢复String[]
中的所有值:
[\{\,](.*?)\=
在online regex tester中,除了结尾}
之外,它的效果很好。
但是当我实际对第一行运行模式时,我得到一个String[]
,其中第一个元素是一个空字符串。我只想要每行的4个值(不是键)而不是额外的空值。
Pattern getRowValues = Pattern.compile("[\\{\\,](.*?)\\=");
String[] row = getRowValues.split("{failures=4, successes=6, name=this_is_a_name, p=40.00}");
//CURRENT
//row[0]=> ""
//row[1]=>"4"
//row[2]=>"6"
//row[3]=>"this_is_a_name"
//row[4]=>"40.00}"
//WANT
//row[0]=>"4"
//row[1]=>"6"
//row[2]=>"this_is_a_name"
//row[3]=>"40.00"
答案 0 :(得分:1)
String[] parts = getRowValues
// Strip off the leading '{' and trailing '}'
.replaceAll("^\\{|\\}$", "")
// then just split on comma-space
.split(", ");
如果您只想要值:
String[] parts = getRowValues
// Strip off the leading '{' and up to (but no including) the first =,
// and the trailing '}'
.replaceAll("^\\{[^=]*|\\}$", "")
// then just split on comma-space and up to (but no including) the =
.split(", [^=]*");
答案 1 :(得分:1)
将正则表达式修改为[{,](.*?)=|[}]
,其中我删除了每个[...]
构造中所有不必要的转义字符,并添加了|[}]
=([^,]*)[,}]
此正则表达式将执行以下操作:
=
后,
或关闭}
现场演示
https://regex101.com/r/yF2gG7/1
示例文字
{failures=4, successes=6, name=this_is_a_name, p=40.00}
捕获论坛
每个匹配都会获得以下捕获组:
=
到,
或}
=
,,
或}
个字符样本匹配
[0][0] = =4,
[0][1] = 4
[1][0] = =6,
[1][1] = 6
[2][0] = =this_is_a_name,
[2][1] = this_is_a_name
[3][0] = =40.00}
[3][1] = 40.00
NODE EXPLANATION
----------------------------------------------------------------------
= '='
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^,]* any character except: ',' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
[,}] any character of: ',', '}'
----------------------------------------------------------------------