Results 1 to 9 of 9
  1. #1
    ehsansad is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default regular expression

    Hello,
    How do you use String.matcher() to catch just a Word. for example if you have this

    SCHEMA XXX_TO_BE FOR SOME OTHER THING. SCHEMA_VER

    how do you match only SCHEMA and not SCHEMA_VER. I have tried \bSCHEMA\b, \s'SCHEMA\s+ without luck and only \s*SCHEMA.* works which matches both

    regards,
    ehsan

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    I have read your post four times, but still do not understand exactly what do you want :D

    Can you explain it again, maybe with an other example? Sorry ;/

    String.matcher - do you mean matches? But this only tells you whether or not the string matches the given regular expression and will not catch a word :confused:

  3. #3
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    I think he's also looking for regular expression which would only match SCHEMA as a word by itself. In which case, \bSCHEMA\b should work fine.

  4. #4
    ehsansad is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    yes matches() is correct.
    say the string is

    String s = "MYSTRING and some other stuff like will be in this line.";

    I want to do some stuff if and only if "MYSTRING" is in the line. so

    if(s.matches("MYSTRING") {
    \\do something here
    }

    I have tried
    s.matches("\bMYSTRING\b") and s.matches("//s*MYSTRING//s+") without any luck. so I want to know how can i match "MYSTRING".

  5. #5
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    You'll want to look at pattern and matcher to do the regular expressions you want. String's matches() method will see if the regex matches the entire string, not just a word. \bMYSTRING\b should work if you use pattern and matcher.

  6. #6
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    String.contains do what you want or?
    To your problem in the first post, what do you mean with
    "only \s*SCHEMA.* works which matches both" ?

    .* is greedy, so that this should find the string only once or?

    \s*SCHEMA.*? should find it twice

    But you can mark the start and end of the string with \b
    --> \s*\bSCHEMA\b.* - do you mean that?

    I don`t understand your problem, sorry :D

  7. #7
    ehsansad is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot, fixed by using the \s*\bSCHEMA\b\s+.

  8. #8
    Melssj5 is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    you should try indexOf instead of matches. The matches method tells you if the String matches a regular expression, the indexOf can tell you if the String has a substring on it! for example

    if (String.indexOf("secondString">0) {
    77then add your desired code.
    }

    I guess that the matches method is not what you are looking for!

  9. #9
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by Melssj5 View Post
    you should try indexOf instead of matches. The matches method tells you if the String matches a regular expression, the indexOf can tell you if the String has a substring on it! for example

    if (String.indexOf("secondString">0) {
    77then add your desired code.
    }
    Thats the same as I said before:

    Quote Originally Posted by eRaaaa View Post
    String.contains .....
    -->
    Java Code:
        public boolean contains(CharSequence s) {
            return indexOf(s.toString()) > -1;
        }
    :cool:

Similar Threads

  1. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 10:01 PM
  2. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 11:15 AM
  3. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 03:55 AM
  4. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 12:25 PM
  5. need help to make this regular expression
    By aruna1 in forum New To Java
    Replies: 1
    Last Post: 04-12-2009, 08:09 PM

Posting Permissions

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