Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pattern matching

 
deep rai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i need to parse a string and need a good pattern to match conditions any help is appreciated. eg :

imported bottle of perfume
packet of headache pills
bottle of perfume
box of imported chocolates
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, we need to understand what you are trying to match. Having 4 generic phrases is not enough to determine what you are trying to match, and more importantly, what you are trying to *not* match.

Second, regex can also parse. So, if you describe *exactly* what you are trying to match, and then describe exactly how it is to be parsed, we may be able to provide a regex that can parse too.

Henry
 
K.Suresh Kumar
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Exactly what do you want?..

Here is some sample code.......

import java.util.regex.Pattern;

public class MainClass {
public static void main(String args[]) {
String statement = "a b c d e f g h i j k l";

String splitPattern = "e|c|a|a|(a b d e)|(b c e)";

Pattern p = Pattern.compile(splitPattern);

String[] tokens = p.split(statement);

for (int i = 0; i < tokens.length; i++) {
System.out.println(tokens[i]);
}
}
}

Regards,
Suresh Kumar.K
 
deep rai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you both Henry and Suresh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic