简体   繁体   中英

String parsing with delimeter using Java Regex

I need to parse my string using double spaces.So i did wrote regex as / /g .This does find all double spaces but i only need the ones such that between every consecutive doublespace there must be "=" sign.To make things clear let me explain with example:

For the input:

Hes id= 000014  Sbkd= 1151  Hesno= 0138075  Tur= 7  Dvkd= TL  name= james michael   king  id= 555  home=turkey

if i try with regex / /g i get the matches as:

正则表达式输出ScreenShot

but as you see i dont need the double space after michael,obviously i don't want to split the name.

So is there any regex which will check the characters between matches and help solve my problem?

Note:I use Java ,but i just need the regex,lets assume i can't use any java functions.

You can use a lookahead in your split regex:

/  (?=\w+=)/g

(?=\\w+=) will make sure there is a <name>= after 2 spaces.

RegEx Demo

In Java you have to use:

String[] arr = input.split("  (?=\\w+=)");

If you are unable to use lookaheads or lookbehinds, another way is to simply check after the double spaces for any phrase, followed by an = sign.

/( )[^= ]+=/ will give you the result you are looking for.

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