I have the following array of regular expressions:
String[] array = new String[] {
"(car)|(truck)|(bus)|(van)", //4) transportation
"(w)|(x)|(y)|(z)", //1) options
"1|2|3|4", //2) numbers
"(red)|(blue)|(green)|(pink)|(yellow)" //3) color
};
and I have the following string:
String s= "1 blue w truck";
I am trying to iterate over this string to see if any of the words in the string match any of the regular expressions in the array. This is what I am doing:
for(int i=0; i<array.length;i++){
Pattern word = Pattern.compile(array[i]);
Matcher match = word.matcher(s);
while(match.find() ){
System.out.println(String.format(" Using regex %d: %s",i,match.group()));
}
}
This gives the following output:
Using regex 0: truck
Using regex 1: w
Using regex 2: 1
Using regex 3: blue
But I want the following to be the output:
Using regex 2: 1
Using regex 3: blue
Using regex 1: w
Using regex 0: truck
I want the words in the strings to stay in the same order without changing the order of the regular expressions in the array.