Regular expression output is not the same

I have a line like this:

test_0001_suiteid_111_leavepolicy_employee

When I split this in java using a regex like:

_(?=.*_)

It shows how it looks:

test
0001 
suiteid
111
leavepolicy_employee

But if I use this line:

test_0001_suiteid_111_leavepolicy

It shows how it looks:

test
0001 
suiteid
111_leavepolicy

Could you explain why this is happening. I want the result to be the same as the first output using a regular regular expression.

+4
source share
2 answers

You say you do it in Java. If you use String#split(), you can use the version with two arguments and provide several elements that you want to return. I assume that the number of key / value pairs in your string is corrected or you know that .

String string = "test_0001_suiteid_111_leavepolicy_employee";
String[] parts = string.split("_", 5);

:

test
0001
suiteid
111
leavepolicy_employee

, test_0001_suiteid_111_leavepolicy.

+1

, , , - - (?=.*_).

, , :

(?<=\d)_|_(?=.*_)

. - live regex

+2

Source: https://habr.com/ru/post/1608689/


All Articles