Searching a pattern

Discussion in 'Java' started by gonas, Nov 20, 2004.

  1. gonas

    gonas Guest

    How can i search for a pattern in a string.

    ie. if the pattern is "xyz" then the string can be tokenized only when
    the pattern "xyz" appears and not for each "x","y","z"

    also the same for multiple patterns like "xyz" and "abc". finding the
    string between the two patterns.

    usage of indexOf is very much killing tell me some other ways
    gonas, Nov 20, 2004
    #1
    1. Advertising

  2. gonas

    Alan Moore Guest

    On 20 Nov 2004 00:57:07 -0800, (gonas) wrote:

    >How can i search for a pattern in a string.
    >
    >ie. if the pattern is "xyz" then the string can be tokenized only when
    >the pattern "xyz" appears and not for each "x","y","z"
    >
    >also the same for multiple patterns like "xyz" and "abc". finding the
    >string between the two patterns.
    >
    >usage of indexOf is very much killing tell me some other ways


    If you're using jdk1.4+, you can use the built-in regular expression
    package (for earlier versions, there are third party packages you can
    use). So, to find all occurrences of "xyz" or abc" you would do this:

    Pattern p = Pattern.compile("xyz|abc");
    Matcher m = p.matcher(inputString);
    while (m.find())
    {
    System.out.println("found: " + m.group());
    }

    Here's a site that has a pretty good regex tutorial:

    http://www.regular-expressions.info/

    Also be sure and read the javadocs for the Pattern and Matcher classes
    (in the java.util.regex package).
    Alan Moore, Nov 21, 2004
    #2
    1. Advertising

  3. gonas

    Oscar kind Guest

    gonas <> wrote:
    > How can i search for a pattern in a string.
    >
    > ie. if the pattern is "xyz" then the string can be tokenized only when
    > the pattern "xyz" appears and not for each "x","y","z"
    >
    > also the same for multiple patterns like "xyz" and "abc". finding the
    > string between the two patterns.
    >
    > usage of indexOf is very much killing tell me some other ways


    What is it exactly that you are trying to do?

    If you're searching for the pattern itself, have a look at the package
    java.util.regex (especially the classes Pattern and Matcher therein).

    If you're using the pattern as a delimiter and want to tokenize the string
    in which it occurs, have a look at java.lang.String#split(String) and
    java.lang.String#split(String,int).


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Nov 21, 2004
    #3
    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. Brian Andrus

    searching for null in pattern

    Brian Andrus, Nov 7, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    73
    Brad Baxter
    Nov 8, 2003
  2. Shiraz

    Pattern Searching

    Shiraz, Jun 22, 2005, in forum: Perl Misc
    Replies:
    15
    Views:
    146
    Gunnar Hjalmarsson
    Jun 26, 2005
  3. jhu
    Replies:
    6
    Views:
    117
    Dave Weaver
    Nov 26, 2007
  4. Peter Stacy
    Replies:
    1
    Views:
    105
    John W. Krahn
    Nov 8, 2009
  5. stumblng.tumblr
    Replies:
    1
    Views:
    204
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page