简体   繁体   中英

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:

_(?=.*_)

It shows ouptut like this:

test
0001 
suiteid
111
leavepolicy_employee

But if I use this string:

test_0001_suiteid_111_leavepolicy

It shows ouptut like this:

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. If you use String#split() , you can use the two-argument version and supply a number of elements you want to get back. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM