Results 1 to 9 of 9
Thread: regular expression
- 03-02-2011, 02:56 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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
- 03-02-2011, 04:18 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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:
- 03-02-2011, 04:32 PM #3
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.
- 03-02-2011, 06:11 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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".
- 03-02-2011, 06:22 PM #5
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.
- 03-02-2011, 06:29 PM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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
- 03-03-2011, 08:14 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
Thanks a lot, fixed by using the \s*\bSCHEMA\b\s+.
- 03-18-2011, 02:20 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
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!
- 03-18-2011, 02:23 PM #9
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Similar Threads
-
Help with regular expression
By mr.ab18 in forum New To JavaReplies: 2Last Post: 08-06-2010, 10:01 PM -
regular expression
By prof.deedee in forum JDBCReplies: 3Last Post: 02-19-2010, 11:15 AM -
regular expression
By QkrspCmptPop in forum Advanced JavaReplies: 8Last Post: 01-20-2010, 03:55 AM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM -
need help to make this regular expression
By aruna1 in forum New To JavaReplies: 1Last Post: 04-12-2009, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks