File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes pattern matching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pattern matching" Watch "pattern matching" New topic

pattern matching

deep rai
Ranch Hand

Joined: Oct 04, 2007
Posts: 56

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

Joined: Sep 28, 2004
Posts: 20531

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
K.Suresh Kumar
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
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++) {

Suresh Kumar.K
deep rai
Ranch Hand

Joined: Oct 04, 2007
Posts: 56
thank you both Henry and Suresh
I agree. Here's the link:
subject: pattern matching
It's not a secret anymore!