[英]regular expression output is not same
I have a string like this: 我有一个像这样的字符串:
test_0001_suiteid_111_leavepolicy_employee
When I split this in java using regular expression like this: 当我使用正则表达式在java中拆分它时,如下所示:
_(?=.*_)
It shows ouptut like this: 它显示了这样的ouptut:
test
0001
suiteid
111
leavepolicy_employee
But if I use this string: 但是如果我使用这个字符串:
test_0001_suiteid_111_leavepolicy
It shows ouptut like this: 它显示了这样的ouptut:
test
0001
suiteid
111_leavepolicy
Can you please explain why this is happening. 你能解释一下为什么会这样吗? I want the output same as first output using a common regular expression.
我希望输出与使用常见正则表达式的第一个输出相同。
Behaviour is as expected, which splits on underscore only if another underscore appears later in the input - due to the look ahead (?=.*_)
. 行为是预期的,只有在输入后面出现另一个下划线时才会在下划线上拆分 - 由于前瞻
(?=.*_)
。
If instead you also want to split if the underscore appears after a digit, use this regex: 相反,如果你也想,如果下划线一个数字后出现分裂,使用这个表达式:
(?<=\d)_|_(?=.*_)
See live regex demo 查看实时正则表达式演示
You say you are doing that in Java. 你说你是用Java做的。 If you use
String#split()
, you can use the two-argument version and supply a number of elements you want to get back. 如果使用
String#split()
,则可以使用双参数版本并提供要返回的许多元素。 I am assuming the number of key/value pairs in your string is fixed or you know it . 我假设您的字符串中的键/值对的数量是固定的或您知道它 。
String string = "test_0001_suiteid_111_leavepolicy_employee";
String[] parts = string.split("_", 5);
That should give you a list of five elements: 这应该给你一个五个元素的列表:
test
0001
suiteid
111
leavepolicy_employee
Equally it will yield five elements if you put in test_0001_suiteid_111_leavepolicy
. 同样,如果你输入
test_0001_suiteid_111_leavepolicy
,它将产生五个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.