Regex: string match against mutiple patterns

Discussion in 'Java' started by thorsten, Mar 10, 2005.

  1. thorsten

    thorsten Guest

    Hi,

    I have a textfile which read line by line. Each line should be
    checked, if there is a match against five different patterns.

    1) How must i write the pattern for five matches

    2) How can i get the text after the match up to the next space

    Perhaps there is somebody who has done this before.

    Thanks for any ideas

    Thorsten
    (Regex Newbie)
    thorsten, Mar 10, 2005
    #1
    1. Advertising

  2. thorsten

    HK Guest

    thorsten wrote:
    > Hi,
    >
    > I have a textfile which read line by line. Each line should be
    > checked, if there is a match against five different patterns.
    >
    > 1) How must i write the pattern for five matches


    import java.util.regex.*;
    Pattern p = Pattern.compile("("+re1+")"
    +"|"...+"|"
    "("+re5+")")
    Matcher m = p.matcher(line_of_text);
    if( m.matches() ) {
    // do what has to be done

    > 2) How can i get the text after the match up to the next space


    int end = m.end();
    Pattern space = Pattern.compile("[^ ]*");
    Matcher ms = space.matcher(line_of_text.substring(end));
    String textUptoSpace = ms.group(0);
    }

    The problem with this solution is, that you don't know which pattern
    actually matched. To know this, you would need an if/elseif/else
    cascade. Of course you also need the loop over the input lines.
    To read a small config file occasionally, there is nothing wrong with
    this approach.

    For high speed/high volume text processing, you may want to try

    http://www.ebi.ac.uk/Rebholz-srv/whatizit/monq-doc/monq/jfa/package-summary.html#package_description

    You basically specify regex/action pairs such that the action is
    called whenever the associated regex is spotted in the input. Looping
    through an input stream is taken care of by the system. You
    concentrate on what has to happen when a specific regex is seen.

    Harald.
    HK, Mar 10, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RV
    Replies:
    1
    Views:
    434
    Donald 'Paddy' McCarthy
    Oct 23, 2003
  2. BogusException
    Replies:
    3
    Views:
    331
    BogusException
    Aug 16, 2006
  3. tomasz
    Replies:
    7
    Views:
    332
    Jonathan Gardner
    Dec 18, 2007
  4. Peng Yu
    Replies:
    1
    Views:
    342
    Alex Willmer
    Aug 7, 2010
  5. RV
    Replies:
    6
    Views:
    125
    Darren Dunham
    Oct 23, 2003
Loading...

Share This Page