index of a regex in a String

Discussion in 'Java' started by Guest, Mar 25, 2005.

  1. Guest

    Guest Guest

    How can I implement this?
    (Without checking characters one-by-one with my code)

    String a = " abc";
    int b = a.indexOfRegex("\s", 0);
    // b is 2 (index of first non-whitespace character)
    Guest, Mar 25, 2005
    #1
    1. Advertising

  2. Guest

    John McGrath Guest

    On 3/24/2005 at 9:04:59 PM, "<- Chameleon ->"
    <> wrote:

    > How can I implement this?
    > (Without checking characters one-by-one with my code)
    >
    > String a = " abc";
    > int b = a.indexOfRegex("\s", 0);
    > // b is 2 (index of first non-whitespace character)


    Note that you need to escape the backslash: "\\s", not "\s".

    Pattern pattern = Pattern.compile( "\\s" );
    Matcher matcher = pattern.matcher( a );
    if ( matcher.find() ) {
    int start = matcher.start();
    } else {
    // not found
    }

    --
    Regards,

    John McGrath
    John McGrath, Mar 25, 2005
    #2
    1. Advertising

  3. Guest

    Alan Moore Guest

    On Fri, 25 Mar 2005 03:03:15 GMT, "John McGrath" <>
    wrote:

    >On 3/24/2005 at 9:04:59 PM, "<- Chameleon ->"
    ><> wrote:
    >
    >> How can I implement this?
    >> (Without checking characters one-by-one with my code)
    >>
    >> String a = " abc";
    >> int b = a.indexOfRegex("\s", 0);
    >> // b is 2 (index of first non-whitespace character)

    >
    >Note that you need to escape the backslash: "\\s", not "\s".
    >
    > Pattern pattern = Pattern.compile( "\\s" );
    > Matcher matcher = pattern.matcher( a );
    > if ( matcher.find() ) {
    > int start = matcher.start();
    > } else {
    > // not found
    > }


    That will return the index of the first *whitespace* character, zero.
    If you want the index of the first non-whitespace character, either
    change the regex to "\\S" or use matcher.end().
    Alan Moore, Mar 25, 2005
    #3
  4. Guest

    John McGrath Guest

    On 3/25/2005 at 1:27:10 AM, Alan Moore wrote:

    > That will return the index of the first whitespace character, zero.
    > If you want the index of the first non-whitespace character, either
    > change the regex to "\\S" or use matcher.end().


    Yes, that is true. I think the OP was looking for how to find the
    location that an arbitrary pattern matches - which is what the code I
    posted does. I was not paying attention to the actual pattern he provided.

    --
    Regards,

    John McGrath
    John McGrath, Mar 25, 2005
    #4
    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. Mladen Adamovic
    Replies:
    0
    Views:
    725
    Mladen Adamovic
    Dec 4, 2003
  2. Replies:
    3
    Views:
    734
    Reedick, Andrew
    Jul 1, 2008
  3. Ruby Newbee

    regex =~ string or string =~ regex?

    Ruby Newbee, Jan 4, 2010, in forum: Ruby
    Replies:
    3
    Views:
    127
    Kirk Haines
    Jan 4, 2010
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    271
    Tomasz Chmielewski
    Mar 4, 2008
  5. Timmy
    Replies:
    3
    Views:
    128
    Timmy
    Nov 4, 2005
Loading...

Share This Page