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
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.
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("")); Matcher m=Pattern.compile("\\d+").matcher(inp); System.out.println(m.matches());
true false true
The output of the testcase shows how it contains method works for regex.