Finding duplicate patterns in a sentence using reqular expressions in java

I want to find duplicate patterns in the following sentence using reqular expressions in java:

username|s:5:"derick256";privilege|s:5:"derick542";premium|s:5:"derik542";

I need to extract the following, and possibly more, so I need a solution that extends easily ...

  • Username derick256
  • privilege derick 542
  • premium derik542

This is my code ...

String re1="((?:[a-z][a-z0-9_]*))"; // Variable Name 1
String re2=".*?";   // Non-greedy match on filler
String re3="(?:[a-z][a-z0-9_]*)";   // Uninteresting: var
String re4=".*?";   // Non-greedy match on filler
String re5="((?:[a-z][a-z0-9_]*))"; // Variable Name 2

Pattern p = Pattern.compile(re1+re2+re3+re4+re5,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(strLine);
if (m.find()){
    String word1=m.group(1);
    String word2=m.group(2);
    System.out.print("("+word1.toString()+")"+"("+word2.toString()+")"+"\n");
}

But I only got it username derick256. Can someone help me figure out the error.

+3
source share
1 answer

Change if(m.find())to while(m.find()).

+4
source

All Articles