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
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
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
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]*";
> 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.