Getting substring by regex

Discussion in 'Java' started by Christine Mayer, Sep 6, 2007.

  1. Hi, I got a String that is composed of digits, white space, numbers
    and other characters.
    Example: 03... London (first two digits of post code, plus 3 dots for
    the remaining digits).

    I want to go through the String and search for the first occurrence of
    a letter (A-Za-Z).
    Then I want the String from this point on, excluding the "post code
    String" containing only numbers, whitespace and dots.
    The class String seems to have a "split(regex) function, but this
    didn't work for me.

    Any idea how this could be done?

    Thanks in advance,

    Christine
     
    Christine Mayer, Sep 6, 2007
    #1
    1. Advertising

  2. Christine Mayer wrote:
    > Hi, I got a String that is composed of digits, white space, numbers
    > and other characters.
    > Example: 03... London (first two digits of post code, plus 3 dots for
    > the remaining digits).
    >
    > I want to go through the String and search for the first occurrence of
    > a letter (A-Za-Z).
    > Then I want the String from this point on, excluding the "post code
    > String" containing only numbers, whitespace and dots.
    > The class String seems to have a "split(regex) function, but this
    > didn't work for me.
    >
    > Any idea how this could be done?
    >
    > Thanks in advance,
    >
    > Christine
    >


    Look at matching for regex:
    http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 6, 2007
    #2
    1. Advertising

  3. Well, I know the Pattern class, but I don't think it could help here.
    You were probably thinking of the split function (Which seems to do
    just the same the String.split function does)

    In the API, it gives the following example:

    The input "boo:and:foo", for example, yields the following results
    with these parameters:

    Regex Limit Result
    : 2 { "boo", "and:foo" }
    : 5 { "boo", "and", "foo" }
    : -2 { "boo", "and", "foo" }
    o 5 { "b", "", ":and:f", "", "" }
    o -2 { "b", "", ":and:f", "", "" }
    o 0 { "b", "", ":and:f" }


    However, in all these examples there is only one character as "regex.
    While in my case I need a whole String as regex, if found, I need to
    chop of this part from the String...
     
    Christine Mayer, Sep 6, 2007
    #3
  4. Christine Mayer wrote:
    > Well, I know the Pattern class, but I don't think it could help here.
    > You were probably thinking of the split function (Which seems to do
    > just the same the String.split function does)


    You obviously did not read the link I gave you. On that page, under the
    heading "Groups and capturing":
    Capturing groups are so named because, during a match, each
    subsequence of the input sequence that matches such a group is saved.
    The captured subsequence may be used later in the expression, via a back
    reference, and may also *be retrieved from the matcher once the match
    operation is complete.* [ My emphasis. ]
    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Sep 6, 2007
    #4
  5. Christine Mayer

    SadRed Guest

    On Sep 7, 6:49 am, Joshua Cranmer <> wrote:
    > Christine Mayer wrote:
    > > Well, I know the Pattern class, but I don't think it could help here.
    > > You were probably thinking of the split function (Which seems to do
    > > just the same the String.split function does)

    >
    > You obviously did not read the link I gave you. On that page, under the
    > heading "Groups and capturing":
    > Capturing groups are so named because, during a match, each
    > subsequence of the input sequence that matches such a group is saved.
    > The captured subsequence may be used later in the expression, via a back
    > reference, and may also *be retrieved from the matcher once the match
    > operation is complete.* [ My emphasis. ]
    > --
    > Beware of bugs in the above code; I have only proved it correct, not
    > tried it. -- Donald E. Knuth


    You don't nedd capturing groups for this simple task.
    ------------------------------------------
    import java.util.regex.*;

    public class ChristineMayer{

    public static void main(String[] args){

    String[] texts = {"03... London",
    "18... Christine",
    "35... Mayer",
    "77... Bagdad"};

    String regx = "[A-Za-z]+"; // substring comosed of Eng. alphabet

    Pattern pat = Pattern.compile(regx);
    for (String s : texts){
    Matcher mat = pat.matcher(s);
    while (mat.find()){
    System.out.println(mat.group());
    }
    }
    }
    }
    ---------------------------------------
     
    SadRed, Sep 7, 2007
    #5
  6. Christine Mayer

    Roedy Green Guest

    On Thu, 06 Sep 2007 09:44:10 -0700, Christine Mayer <>
    wrote, quoted or indirectly quoted someone who said :

    >The class String seems to have a "split(regex) function, but this
    >didn't work for me.


    see http://mindprod.com/jgloss/regex.html

    See the section on matching vs finding.

    You might find this easier to do by a char by char stepping through
    the string. Write yourself a method that categorizes a char and
    returns an enum, e.g. ALPHA, NUM, DOT, OTHER to use is the loop.

    see http://mindprod.com/jgloss/finitestate.html


    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 7, 2007
    #6
    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. Christine Mayer

    Getting substring by regex

    Christine Mayer, Sep 6, 2007, in forum: Java
    Replies:
    0
    Views:
    334
    Christine Mayer
    Sep 6, 2007
  2. Christine Mayer

    Getting substring by regex

    Christine Mayer, Sep 6, 2007, in forum: Java
    Replies:
    0
    Views:
    301
    Christine Mayer
    Sep 6, 2007
  3. Replies:
    3
    Views:
    770
    Reedick, Andrew
    Jul 1, 2008
  4. Timur Tabi
    Replies:
    4
    Views:
    923
    Timur Tabi
    Oct 14, 2009
  5. Replies:
    3
    Views:
    204
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page