If I use string.contains() on a string that may have regular expressions

If I use string.contains() on a string that may have regular expressions, will the regular expressions trigger? For example string.contains("alpha\wbet").

Am I right in thinking that contains will be fine but matches will not? Thank you

Answers


String.contains() accepts CharSequence as argument and doesn't "trigger" regexes that it or processing string may contain.

Returns true if and only if this string contains the specified sequence of char values.

From here.


Contains take a string as parameter and looks for that string. It doesn't work as regexp. If you want to search for regular experssion use string.matches


It accepts char sequence as a paramater. If you give regex contains method consider that as char sequence. Below provide sample program depict the behavior.

 String inp="12345";
 System.out.println(inp.contains("34"));
 System.out.println(inp.contains("[34]"));
 Matcher m=Pattern.compile("\\d+").matcher(inp);
 System.out.println(m.matches());

Output:

 true
 false
 true

The output of the testcase shows how it contains method works for regex.


Need Your Help

Wait until tomcat finishes starting up

java linux tomcat

I have a script that needs to run after tomcat has finished starting up and is ready to start deploying applications. I'm using $TOMCAT_HOME/bin/startup.sh which returns immediately. How can I wait...

Can't create new project from existing in PHPStorm

project phpstorm

I just installed PHPStorm 6.0.3 on my mac (Lion 10.7.5). I successfully added one project via New Project From Existing (SFTP), but when I try to add a second New Project From Existing (also SFTP)...