java - Regex for a string pattern split -


have scenario strip below pattern strings -

abc|hjdj|kleygag|0|0|0|0| ghys|jkugb|0|0|0 yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0| 

looking regex in java separate these values excluding first occurence of pipe before 0, output should like-

abc|hjdj|kleygag ghyd|jkugb yuubf|kluygb|tyrffv|nutgv 

just need regex, has been answered , been helpful, helpful know regex , not complete code reverse pattern requested in followup question

is regex necessary this? substring() gets want want easily.

update

i saw comment you're wanting case data looks like, "0|0|0|0|abdc|ghyft|rtyu". i've modified answer account case , case data be, "0|0|0|0|abdc|ghyft|rtyu|0|0|0|"

either way:

public static void main(string[] args) throws exception {     list<string> strings = new arraylist(){         {             add("abc|hjdj|kleygag|0|0|0|0|");             add("ghys|jkugb|0|0|0");             add("yuubf|kluygb|tyrffv|nutgv|0|0|0|0|0|");             add("0|0|0|0|abdc|ghyft|rtyu");             add("0|0|0|0|abdc|ghyft|rtyu|0|0|0|0|0|");         }     };      // non regex     system.out.println("non regex");     (string string : strings) {         int startindex = -1;         int endindex = -1;         // find first non 0 character         (int = 0; < string.length(); i++) {             if ('a' <= string.charat(i) && string.charat(i) <= 'z') {                 startindex = i;                 break;             }         }         // find first pipe 0 |0 after startindex         endindex = string.indexof("|0", startindex);          // determine substring() use based on endindex results         system.out.println(endindex > -1 ? string.substring(startindex, endindex) : string.substring(startindex));     }     system.out.println("");      // regex     system.out.println("regex");     (string string : strings) {         system.out.println(string.replaceall("\\|0|0\\||\\|$", ""));     } } 

results:

non regex

abc|hjdj|kleygag ghys|jkugb yuubf|kluygb|tyrffv|nutgv abdc|ghyft|rtyu abdc|ghyft|rtyu  regex abc|hjdj|kleygag ghys|jkugb yuubf|kluygb|tyrffv|nutgv abdc|ghyft|rtyu abdc|ghyft|rtyu 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -