sriram_adapa
sriram_adapa

Reputation: 133

Finding repeating patterns in a sentence using reqular expressions in java

I want to find repeating 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 potentially more, so I need a solution that is easily expandable...

  1. username derick256
  2. privilege derick 542
  3. 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 username derick256. Could anyone please assist me to understand the error.

Upvotes: 3

Views: 835

Answers (1)

Neil
Neil

Reputation: 5780

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

Upvotes: 4

Related Questions