Results 1 to 4 of 4
  1. #1
    jove is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default How to use multiple different patterns in java?

    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. if in my string I have Price or Time , show both of them , I mean i need both of them .

    thanks for your help....

    here is my code :

    Java 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");
          }
    Last edited by jove; 05-04-2013 at 01:19 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How to use multiple different patterns in java?

    You could scan the String with multiple patterns and accumulate the results.

    Note that there may be more than one time or amount (as defined by your patterns) so you could end up with three results. Even if you think this won't be the case for your input you should check for and respond to finding any number of matches (from zero up).

  3. #3
    jove is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Unhappy Re: How to use multiple different patterns in java?

    Dear pbrockway2 i tried to change to these code but it doesn't give me right output

    here is codes :::


    // String to be scanned to find the pattern.
    String line = "This order was places for QT 30.00$ !OK ? 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) + " with time: " + m.group(1));
    }
    Last edited by jove; 05-04-2013 at 08:50 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to use multiple different patterns in java?

    I agree with Pbrockway2: don't 'overdo' regular expressions; in your case it's easier to use two separate (simpler) REs for the two patterns instead of one. People tend to think that REs have super powers; they don't and you may end up with a RE that seems to work but happens to be dead slow and overly complicated. They're just a computational implementation of a regular language ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. patterns
    By chameera in forum New To Java
    Replies: 2
    Last Post: 05-01-2013, 09:12 AM
  2. Replies: 0
    Last Post: 04-06-2012, 09:24 PM
  3. Replies: 1
    Last Post: 12-26-2011, 02:50 PM
  4. Java design patterns
    By ajeeb in forum New To Java
    Replies: 4
    Last Post: 03-03-2009, 06:10 PM
  5. Java Mashup API - Design Patterns Questionnaire
    By goldeneyeone in forum Reviews / Advertising
    Replies: 0
    Last Post: 02-10-2009, 11:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •