How to use multiple different patterns?

java

Solution

The "+" operator does not separate patterns - it concatenates strings.

What you can do is provide a pattern that accepts characters in between the two groups.

  String pattern = "(\\d+[.,]\\d+.[$]).*(\\d:\\d\\d)";

The parentheses above are optional. If you include them, you can get the matched price and time as separate strings:

 if (m.find( )) {
     System.out.println("Found value: " + m.group(1) + " with time: " + m.group(2));
 }

EDIT:

Just noticed your comment that you're looking for OR, not AND.

You can do that with an expression of the form `X | Y`:

  String pattern = "\\d+[.,]\\d+.[$]|\\d:\\d\\d";

This will match either a price or a time, whichever occurs first. You can get the match with `m.group(0)`.

Problem

how to check strings for multi-pattern regex not for single pattern if tried for one pattern but I need it for multi-pattern and i tried but it doesn't work. when I running these codes just I can get one of them (time or price ) that is in the String but when I combine them don't show me any output. thanks for your help.... here is my code : ``` String line = "This order was places for QT 30.00$ ! OK? and time is 2:45"; String pattern = "\\d+[.,]\\d+.[$]"+"\\d:\\d\\d"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(line); if (m.find( )) { System.out.println("Found value: " + m.group(0) ); } else { System.out.println("NO MATCH"); } ```

Original source