I have a string first=abc123,second=status,z=3,a=45745
and want to get an output abc123,status,3,45745
.
我有一个字符串first = abc123,second = status,z = 3,a = 45745并且想获得输出abc123,status,3,45745。
I could try to split it into multiple arrays and the get the values OR put this string into an ArrayList and split to get the result, but wanted to check if somebody already has a solution which does not involve a lot array creation or manipulation. Like a regex which could be helpful.
我可以尝试将其拆分为多个数组并获取值或将此字符串放入ArrayList并拆分以获取结果,但是想检查某人是否已经有一个不涉及大量数组创建或操作的解决方案。就像一个可能有用的正则表达式。
~TIA
2 个解决方案
#1
1
.*?=([a-zA-Z0-9]*)
在Debuggex上实时编辑
This will work. However, to make it better is there some constraints to the characters after or before the = sign?
这会奏效。但是,为了使它更好,是否在=符号之后或之前对字符有一些限制?
For example,
[a-zA-Z]*?=([a-zA-Z0-9]*)
This will only check if there is only letters at the start of the equals sign then letters/numbers after that and ignore the , all together. If it doesn't meet those requirements it will fail.
这只会检查等号开头是否只有字母,然后是字母/数字,并忽略所有字母。如果它不符合这些要求,它将失败。
If you know there is never going to be a number = to something i would suggest thing answer.
如果你知道永远不会有一个数字=我会建议回答的事情。
NOTE: Everything you will need will be captured in what regex's calls a capture group which is denoted by ().
注意:您将需要的所有内容都将在正则表达式调用捕获组中捕获,该捕获组由()表示。
#2
0
Description
This expression will return just the value portion of your key value sets as an array of value.
此表达式将仅返回键值集的值部分作为值数组。
(?<=\w=)[^,]*
Live Demo
(?<= \ w =)[^,] *现场演示
Sample Text
first=abc123,second=status,z=3,a=45745
Matches
[0][0] = abc123
[1][0] = status
[2][0] = 3
[3][0] = 45745
#1
1
.*?=([a-zA-Z0-9]*)
在Debuggex上实时编辑
This will work. However, to make it better is there some constraints to the characters after or before the = sign?
这会奏效。但是,为了使它更好,是否在=符号之后或之前对字符有一些限制?
For example,
[a-zA-Z]*?=([a-zA-Z0-9]*)
This will only check if there is only letters at the start of the equals sign then letters/numbers after that and ignore the , all together. If it doesn't meet those requirements it will fail.
这只会检查等号开头是否只有字母,然后是字母/数字,并忽略所有字母。如果它不符合这些要求,它将失败。
If you know there is never going to be a number = to something i would suggest thing answer.
如果你知道永远不会有一个数字=我会建议回答的事情。
NOTE: Everything you will need will be captured in what regex's calls a capture group which is denoted by ().
注意:您将需要的所有内容都将在正则表达式调用捕获组中捕获,该捕获组由()表示。
#2
0
Description
This expression will return just the value portion of your key value sets as an array of value.
此表达式将仅返回键值集的值部分作为值数组。
(?<=\w=)[^,]*
Live Demo
(?<= \ w =)[^,] *现场演示
Sample Text
first=abc123,second=status,z=3,a=45745
Matches
[0][0] = abc123
[1][0] = status
[2][0] = 3
[3][0] = 45745