regular expression

hi! i want to test if a substring is inside another string. i tried:

boolean matches = s1.matches(".+" + s2 +".+");

but it didnt work. how can i get the think to work? i do not kow much about regular expressions.

thx in advance

[336 byte] By [codymanix] at [2007-9-27 18:11:29]
# 1
try using 'equals()'
Sum-shusSue at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 2

Hello,

Would this work for you:

Pattern p = Patter.compile("*abc*");

Matcher m = p.matcher("zzzabczzz");

boolean b = m.matches();

or

boolean b = Pattern.matches("*abc*", "zzzabczzz");

Where "abc" is the string you want to find in "zzzabczzz"

Hope this helps.

-- BB

bjburns at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 3
does not work this way. i get a PatternSyntaxException.java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0*h*^
codymanix at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 4
> i want to test if a substring is inside another stringUse String.indexOf.
YATArchivist at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 5
no i want to create a dialog with can be used to search text in a file. it should accept regular expressions as input be be most flexible.
codymanix at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 6
Okay. Just use s2 as your pattern.Caveat: if you want s2 to be a literal string rather than a regex, you will have to escape the characters like * which have special meaning.
YATArchivist at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 7

now i know what was the problem. my original code

boolean matches = s1.matches(".+" + s2 + ".+");

did not care about linebreaks.

so i think i either have to trim() the string before or use a pattern like

string pattern =

"[.|\\f|\\r|\\n|\\r\\n]*" + s2 + "[.|\\f|\\r|\\n|\\r\\n]*";

codymanix at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 8
or is there a simpler way?
codymanix at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 9
What you want is find(), not matches():boolean matches = Pattern.compile(s2).matcher(s1).find();
uncle_alice at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 10

> now i know what was the problem. my original code

> boolean matches = s1.matches(".+" + s2 + ".+");

> did not care about linebreaks.

>

> so i think i either have to trim() the string before

> or use a pattern like

> string pattern =

> "[.|\\f|\\r|\\n|\\r\\n]*" + s2 +

> "[.|\\f|\\r|\\n|\\r\\n]*";

From the java docs for pattern

The regular expression . matches any character except a line terminator unless the DOTALL flag is specified.

jschell at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...
# 11
Wow, thank you so much, I would've never even considered looking for that find method.
gjamil at 2007-7-6 14:37:40 > top of Java-index,Archived Forums,Java Programming...