contains() method of String class does not take regular expression as a parameter, it takes normal text.
String s = "test.test";
if(s.contains("."))
{
System.out.println("string contains dot");
}
Answer from HaveNoDisplayName on Stack Overflowcontains() method of String class does not take regular expression as a parameter, it takes normal text.
String s = "test.test";
if(s.contains("."))
{
System.out.println("string contains dot");
}
String#contains receives a plain CharacterSequence e.g. a String, not a regex. Remove the \\ from there.
String s = "test.test";
if (s.contains(".")) {
System.out.println("string contains dot");
}
escape the dot, or else it will match any character. This escaping is necessary, because replaceAll() treats the first paramter as a regular expression.
customerName = customerName.replaceAll("\\.", "");
You can do the whole thing with one statement:
customerName = customerName.replaceAll("[\\s.]", "");
use this in your code just for remove periods
customerName = customerName.replaceAll("[.]","");
How do I check if a string contains a dot in Python?
How do you check if a string contains a phrase?
How do you check if a string contains a character?
A dot in a regular expression means "any character". So if you want actual dots you need to escape it with a backslash - and then you need to escape the backslash within Java itself...
if (myString.matches("\\.{1,2}?")){
Next you need to understand that matches tries to match the whole text - it's not just trying to find the pattern within the string.
It's not entirely clear what your requirements are - is it just "more than one dot together" or "more than one dot anywhere in the text"? If it's the latter, this should do it:
if (myString.matches(".*\\..*\\..*")) {
... in other words, anything, then a dot, then anything, then a dot, then anything - where the "anything" can be "nothing". So this will match "He..llo" and "H.e.llo", but not "He.llo".
Hopefully that's what you want. If you literally just want "it must have .. in it somewhere" then it's easier:
if (myString.contains(".."))
As mentioned in the other answers, your regex contains a bug. You could either fix that (escape the dot), or use String.contains instead:
if (myString.contains("..")){
System.out.print("it works");
}
This will match for two consecutive dots. If you're also looking for two dots anywhere in the string (like in the string "He.llo.") you could do
if(myString.indexOf('.', myString.indexOf('.') + 1) != -1) {
System.out.print("it works"); //there are two or more dots in this string
}
Use this regular expression
text.matches("\\d+|\\d*\\.\\d+|\\d+\\.\\d*")
\\d+matches only digits\\d*\\.\\d+matches strings that start either with dot or digit and contain a dot\\d+\\.\\d*matches strings that start with digit, contain a dot and any number of digits after it
Keep in mind that numbers starting with 0 will also be accepted by this expression.
You can simply use
text.equals(".")
or, to take whitespaces into account,
text.trim().equals(".")
[A-Z.]+ should do the trick. Note, that you also don't want small letters.
^ and $ are not required because String.matches operates on the complete String.
The . requires no escape, because within character classes (the [...] part) a . has no special meaning.
Here is another fancy regex:
"my.String".matches("[\\p{script=latin}.]+")
If you want to accept non-Latin letters (other alphabets) too:
"my.String.رشته".matches("[\\p{javaLetter}.]+")
// OR
"my.String.رشته".matches("[\\p{L}.]+")
// Uppercase only:
"MY.STRING".matches("[\\p{Lu}.]+")