It's my first time to use the Pattern class of Java because I want to check a string if it is in uppercase. Special symbols like "." and "," should be treated as Uppercase. Here are the expected results:
"test,." should return false //because it has a lowercase character
"TEST,." should return true //because all are uppercase and the special characters
"test" should return false //because it has a lowercase character
"TEST" should return true //because all are uppercase
"teST" should return false //because it has a lowercase character
I tried to use the StringUtils of apache but it doesn't work this way..
You can check:
if (str.toUpperCase().equals(str)) {..}
Just search for [a-z] then return false if it's found:
if (str.matches(".*[a-z].*")) {
// Negative match (false)
}
Alternatively, search for ^[^a-z]*$ (not sure on Java regex syntax, but basically whole string is not lowercase characters):
if (str.matches("[^a-z]*")) {
// Positive match (true)
}
You could iterate through the chars. It has the advantage that you can customize the matching as you wish.
boolean ok = true;
for(char c : yourString.toCharArray()) {
if(Character.isLetter(c) && !Character.isUpperCase(c)) {
ok = false;
break;
}
}
// ok contains your return value
Related
Words with at least 2 Capital letters and with any special letters (like ##$%^&*()_-+= and so on...) optional.
I tried:
public static boolean isWordHas2Caps(String s) {
return s.matches("\\b(?:\\p{Ll}*\\p{Lu}){2,}\\p{Ll}*\\b");
}
But, I am getting
System.out.println(isWordHas2Caps("eHJHJK"));
System.out.println(isWordHas2Caps("YUIYUI"));
System.out.println(isWordHas2Caps("LkfjkdJkdfj"));
System.out.println(isWordHas2Caps("LLdkjkd"));
System.out.println(isWordHas2Caps("OhdfjhdsjO"));
System.out.println(isWordHas2Caps("LLLuoiu9898"));
System.out.println(isWordHas2Caps("Ohdf&jh/dsjO"));
System.out.println(isWordHas2Caps("auuuu"));
System.out.println(isWordHas2Caps("JJJJJJJJ"));
System.out.println(isWordHas2Caps("YYYY99999"));
System.out.println(isWordHas2Caps("ooooPPPP"));
Output:
true eHJHJK
true YUIYUI
true LkfjkdJkdfj
true LLdkjkd
true OhdfjhdsjO
false LLLuoiu9898 It should be true but getting false
false Ohdf&jh/dsjO It should be true but getting false
false auuuu
true JJJJJJJJ
false YYYY99999 It should be true but getting false
true ooooPPPP
I think, I should in the regexp and numbers and Special letters. How can I do that?
Update:
A valuable comment from anubhava:
Probably s.matches("(?:\\S*\\p{Lu}){2}\\S*"); may be better
Demo of the above solution.
Original answer:
You can use the regex, \b.*\p{Lu}.*\p{Lu}.*\b as shown below:
public static boolean isWordHas2Caps(String s) {
return s.matches("\\b.*\\p{Lu}.*\\p{Lu}.*\\b");
}
Demo:
public class Main {
public static void main(String[] args) {
System.out.println(isWordHas2Caps("eHJHJK"));
System.out.println(isWordHas2Caps("YUIYUI"));
System.out.println(isWordHas2Caps("LkfjkdJkdfj"));
System.out.println(isWordHas2Caps("LLdkjkd"));
System.out.println(isWordHas2Caps("OhdfjhdsjO"));
System.out.println(isWordHas2Caps("LLLuoiu9898"));
System.out.println(isWordHas2Caps("Ohdf&jh/dsjO"));
System.out.println(isWordHas2Caps("auuuu"));
System.out.println(isWordHas2Caps("JJJJJJJJ"));
System.out.println(isWordHas2Caps("YYYY99999"));
System.out.println(isWordHas2Caps("ooooPPPP"));
}
public static boolean isWordHas2Caps(String s) {
return s.matches("\\b.*\\p{Lu}.*\\p{Lu}.*\\b");
}
}
Output:
true
true
true
true
true
true
true
false
true
true
true
You want to check if there are at least two uppercase letters anywhere in a string that can contain arbitrary chars.
Then, you can use
public static boolean isWordHas2Caps(String s) {
return Pattern.compile("\\p{Lu}\\P{Lu}*\\p{Lu}").matcher(s).find();
}
See the Java demo.
Alternatively, if you still want to use String#matches you can use the following (keeping in mind that we need to match the entire string):
public static boolean isWordHas2Caps(String s) {
return s.matches("(?s)(?:\\P{Lu}*\\p{Lu}){2}.*");
}
The (?s)(?:\\P{Lu}*\\p{Lu}){2}.* regex matches
(?s) - the Pattern.DOTALL embedded flag option (makes . match any chars)
(?:\P{Lu}*\p{Lu}){2} - two occurrences of any zero or more chars other than uppercase letters and then an uppercase letter
.* - the rest of the string.
Your code did not return expected results because all of them contain non-letter characters, while String#matches() requires a full string match against a pattern, and yours matches strings that contains letters only.
That is why you should
Make sure you can match anywhere inside a string, and Matcher.find does this job best
\p{Lu}\P{Lu}*\p{Lu} pattern will find any sequence of an uppercase letter + any zero or more non-letters + an uppercase letter
Alternatively, you can use (?s)(?:\P{Lu}*\p{Lu}){2}.* regex to match a full string that contains at least two uppercase letters.
In JAVA (Android), I'm trying to determine in a String, if each character has a equivalent in upper or lower case.
My goal is not to lower or upper the case, but to know if it's possible.
For example this function would return true for : 'e' 'é' 'i' 'l' 'L' 'O' 'P'
and false for emojis or chinese characters.
Is there any function that can do this?
EDIT : To be more clear, the function was supposed to take a character for argument, not a String and return false if the character had no uppercase or lowercase version.
You can try this:
boolean validate(char c){
return Character.isUpperCase(c) || Character.isLowerCase(c);
}
This will return true iff it is a Letter in uppercase or lower case only. Otherwise it'll return false.
The requirements are still not entirely specified (do you care whether the upper/lowercase equivalent is a different character from the original?), but my most straightforward interpretation of the question is:
For each character ch in a given string, is it true that either toUpperCase(ch) yields an uppercase character, or that toLowerCase(ch) yields a lowercase character?
I phrase it that way because Character.toUpperCase() returns "the uppercase equivalent of the character, if any; otherwise, the character itself".
The doc for String.toUppercase() doesn't mention what happens if there is no uppercase equivalent for some characters, but I think we can assume it returns those characters unchanged, as does Character.toUpperCase().
So a straightforward implementation of that condition would be to test
Character.isUpperCase(s.toUpperCase().charAt(0)) ||
Character.isLowerCase(s.toLowerCase().charAt(0));
for each character as a String.
I'm using the String rather than Character case conversion functions here, in order to take advantage of locale-sensitive mapping. Not only that, but regardless of locale, there are characters that cannot be converted to uppercase by Character.toUpperCase() because their uppercase equivalent is more than one character! For example, we would get incorrect results for \u00df 'ß' (see docs for details).
public class TestUpper {
public static void main(String[] args) {
final String test = "\u0633\u0644\u0627\u0645 World \u00df\u01c8eéilLOP\u76f4!";
for (Character ch : test.toCharArray()) {
System.out.format("'%c' (U+%04x): hasCase()=%b%n", ch, (int)ch, hasCase(ch));
}
}
static boolean hasCase(Character ch) {
String s = ch.toString();
// Does the character s have an uppercase or a lowercase equivalent?
return Character.isUpperCase(s.toUpperCase().charAt(0)) ||
Character.isLowerCase(s.toLowerCase().charAt(0));
}
}
And the results:
'س' (U+0633): hasCase()=false
'ل' (U+0644): hasCase()=false
'ا' (U+0627): hasCase()=false
'م' (U+0645): hasCase()=false
' ' (U+0020): hasCase()=false
'W' (U+0057): hasCase()=true
'o' (U+006f): hasCase()=true
'r' (U+0072): hasCase()=true
'l' (U+006c): hasCase()=true
'd' (U+0064): hasCase()=true
' ' (U+0020): hasCase()=false
'ß' (U+00df): hasCase()=true
'Lj' (U+01c8): hasCase()=true
'e' (U+0065): hasCase()=true
'é' (U+00e9): hasCase()=true
'i' (U+0069): hasCase()=true
'l' (U+006c): hasCase()=true
'L' (U+004c): hasCase()=true
'O' (U+004f): hasCase()=true
'P' (U+0050): hasCase()=true
'直' (U+76f4): hasCase()=false
'!' (U+0021): hasCase()=false
These test cases include Arabic letters and a Chinese character (which are isLetter(), but have no upper/lowercase equivalents), the requested test letters, space and punctuation, and a titlecase letter.
The results are correct according to the criteria currently stated in the question. However, the OP has said in comments that he wants the function to return false for titlecase characters, such as U+01c8, whereas the above code returns true because they have uppercase and lowercase equivalents (U+01c7 and U+01c9). But the OP's statement seems to be based on the mistaken impression that titlecase letters do not have uppercase and lowercase equivalents. Ongoing discussion has not yet resolved the confusion.
Disclaimer: This answer doesn't attempt to take into account supplementary or surrogate code points.
For a simple method, there's Character.isLowerCase. But you actually need to be careful- it depends on language. Some languages may have a lower case 'é' but no uppercase. Or like the turkish "I" may have a different lower case version than other languages.
To work around that, I'd use something like Character.isLetter(myChar) && String.valueOf(myChar).toLowerCase().equals(String.valueOf(myChar)). Remember to use the version of toLowerCase that takes a Locale as parameter if not comparing in the default Locale.
Check if the character is either a lowercase letter or an uppercase letter:
Character.isLowerCase(ch) != Character.isUpperCase(ch)
Alternatively, you can compare the lower and uppercased forms of the character:
Character.toLowerCase(ch) == Character.toUpperCase(ch)
However, you need to be careful about locale (there is one letter in Turkish where I think the lower and uppercase forms are the same).
Two strings uppercase and lowercase not matching does not necessarily mean the string is valid. true1 will not equal TRUE1 but fails the test case. You need to check each individual character. This is a rough cut, you'll probably have to do something fancy for emojis and Chinese characters.
public static boolean isAllCase(String value) {
String upper = value.toUpperCase();
String lower = value.toLowerCase();
if(upper.length() != lower.length())
return false;
for(int i = 0; i < upper.length(); i++) {
if(upper.charAt(i) == lower.charAt(i))
return false;
}
return true;
}
public boolean hasEquivalentCase(char ch) {
return (Character.isLowerCase(ch)) || Character.isUpperCase(ch)
}
public boolean validate(char value){
if( (value >= 'a' && value <= 'z') || (value >= 'A' &&
value <= 'Z')
return true;
return false;
}
this for each caracter to your String.
public boolean All( String cad ){
for( int i = 0; i < cad.lenght() ; i++ ){
if( !validate(cad.charAt(i)) ){
#the letter has not upper or lower
return false;
}
}
return true;
}
I have the following method to check that string contains only latin symbols.
private boolean containsNonLatin(String val) {
return val.matches("\\w+");
}
But it returns false if I pass string: my string because it contains space.
But I need the method which will check that if string contains letters not in Latin alphabet it should return false and it should return true in all other cases.
Please help to improve my method.
examples of valid strings:
w123.
w, 12
w#123
dsf%&#
You can use \p{IsLatin} class:
return !(var.matches("[\\p{Punct}\\p{Space}\\p{IsLatin}]+$"));
Java Regex Reference
I need something like not p{IsLatin}
If you need to match all letters but Latin ASCII letters, you can use
"[\\p{L}\\p{M}&&[^\\p{Alpha}]]+"
The \p{Alpha} POSIX class matches [A-Za-z]. The \p{L} matches any Unicode base letter, \p{M} matches diacritics. When we add &&[^\p{Alpha}] we subtract these [A-Za-z] from all the Unicode letters.
The whole expression means match one or more Unicode letters other than ASCII letters.
To add a space, just add \s:
"[\\s\\p{L}\\p{M}&&[^\\p{Alpha}]]+"
See IDEONE demo:
List<String> strs = Arrays.asList("w123.", "w, 12", "w#123", "dsf%&#", "Двв");
for (String str : strs)
System.out.println(!str.matches("[\\s\\p{L}\\p{M}&&[^\\p{Alpha}]]+")); // => 4 true, 1 false
Just add a space to your matcher:
private boolean isLatin(String val) {
return val.matches("[ \\w]+");
}
User this :
public static boolean isNoAlphaNumeric(String s) {
return s.matches("[\\p{L}\\s]+");
}
\p{L} means any Unicode letter.
\s space character
I recently had an interview with Google for a Software Engineering position and the question asked regarded building a pattern matcher.
So you have to build the
boolean isPattern(String givenPattern, String stringToMatch)
Function that does the following:
givenPattern is a string that contains:
a) 'a'-'z' chars
b) '*' chars which can be matched by 0 or more letters
c) '?' which just matches to a character - any letter basically
So the call could be something like
isPattern("abc", "abcd") - returns false as it does not match the pattern ('d' is extra)
isPattern("a*bc", "aksakwjahwhajahbcdbc"), which is true as we have an 'a' at the start, many characters after and then it ends with "bc"
isPattern("a?bc", "adbc") returns true as each character of the pattern matches in the given string.
During the interview, time being short, I figured one could walk through the pattern, see if a character is a letter, a * or a ? and then match the characters in the given string respectively. But that ended up being a complicated set of for-loops and we didn't manage to come to a conclusion within the given 45 minutes.
Could someone please tell me how they would solve this problem quickly and efficiently?
Many thanks!
Assuming you are allowed to use regexes, you could have written something like:
static boolean isPattern(String givenPattern, String stringToMatch) {
String regex = "^" + givenPattern.replace("*", ".*").replace("?", ".") + "$";
return Pattern.compile(regex).matcher(stringToMatch).matches();
}
"^" is the start of the string
"$" is the end of the string
. is for "any character", exactly once
.* is for "any character", 0 or more times
Note: If you want to restrict * and ? to letters only, you can use [a-zA-Z] instead of ..
boolean isPattern(String givenPattern, String stringToMatch) {
if (givenPattern.empty)
return stringToMatch.isEmpty();
char patternCh = givenPatter.charAt(0);
boolean atEnd = stringToMatch.isEmpty();
if (patternCh == '*') {
return isPattenn(givenPattern.substring(1), stringToMatch)
|| (!atEnd && isPattern(givenPattern, stringToMatch.substring(1)));
} else if (patternCh == '?') {
return !atEnd && isPattern(givenPattern.substring(1),
stringToMatch.substring(1));
}
return !atEnd && patternCh == stringToMatch.charAt(0)
&& isPattern(givenPattern.substring(1), stringToNatch.subtring(1);
}
(Recursion being easiest to understand.)
I need a regex which will satisfy both conditions.
It should give me true only when a String contains both A-Z and 0-9.
Here's what I've tried:
if PNo[0].matches("^[A-Z0-9]+$")
It does not work.
I suspect that the regex below is slowed down by the look-around, but it should work regardless:
.matches("^(?=.*[A-Z])(?=.*[0-9])[A-Z0-9]+$")
The regex asserts that there is an uppercase alphabetical character (?=.*[A-Z]) somewhere in the string, and asserts that there is a digit (?=.*[0-9]) somewhere in the string, and then it checks whether everything is either alphabetical character or digit.
It easier to write and read if you use two separate regular expressions:
String s = "blah-FOO-test-1-2-3";
String numRegex = ".*[0-9].*";
String alphaRegex = ".*[A-Z].*";
if (s.matches(numRegex) && s.matches(alphaRegex)) {
System.out.println("Valid: " + input);
}
Better yet, write a method:
public boolean isValid(String s) {
String n = ".*[0-9].*";
String a = ".*[A-Z].*";
return s.matches(n) && s.matches(a);
}
A letter may be either before or after the digit, so this expression should work:
(([A-Z].*[0-9])|([0-9].*[A-Z]))
Here is a code example that uses this expression:
Pattern p = Pattern.compile("(([A-Z].*[0-9])|([0-9].*[A-Z]))");
Matcher m = p.matcher("AXD123");
boolean b = m.find();
System.out.println(b);
Here is the regex for you
Basics:
Match in the current line of string: .
Match 0 or any amount of any characters: *
Match anything in the current line: .*
Match any character in the set (range) of characters: [start-end]
Match one of the regex from a group: (regex1|regex2|regex3)
Note that the start and end comes from ASCII order and the start must be before end. For example you can do [0-Z], but not [Z-0]. Here is the ASCII chart for your reference
Check the string against regex
Simply call yourString.matches(theRegexAsString)
Check if string contains letters:
Check if there is a letter: yourString.matches(".*[a-zA-Z].*")
Check if there is a lower cased letter: yourString.matches(".*[a-z].*")
Check if there is a upper cased letter: yourString.matches(".*[A-Z].*")
Check if string contains numbers:
yourString.matches(".*[0-9].*")
Check if string contains both number and letter:
The simplest way is to match twice with letters and numbers
yourString.matches(".*[a-zA-Z].*") && yourString.matches(".*[0-9].*")
If you prefer to match everything all together, the regex will be something like: Match a string which at someplace has a character and then there is a number afterwards in any position, or the other way around. So your regex will be:
yourString.matches(".*([a-zA-Z].*[0-9]|[0-9].*[a-zA-Z]).*")
Extra regex for your reference:
Check if the string stars with letter
yourString.matches("[a-zA-Z].*")
Check if the string ends with number
yourString.matches(".*[0-9]")
This should solve your problem:
^([A-Z]+[0-9][A-Z0-9]*)|([0-9]+[A-Z][A-Z0-9]*)$
But it's unreadable. I would suggest to first check input with "^[A-Z0-9]+$", then check with "[A-Z]" to ensure it contains at least one letter then check with "[0-9]" to ensure it contains at least one digit. This way you can add new restrictions easily and code will remain readable.
What about ([A-Z].*[0-9]+)|([0-9].*[A-Z]+) ?
Try using (([A-Z]+[0-9])|([0-9]+[A-Z])) .It should solve.
use this method:
private boolean isValid(String str)
{
String Regex_combination_of_letters_and_numbers = "^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$";
String Regex_just_letters = "^(?=.*[a-zA-Z])[a-zA-Z]+$";
String Regex_just_numbers = "^(?=.*[0-9])[0-9]+$";
String Regex_just_specialcharachters = "^(?=.*[##$%^&+=])[##$%^&+=]+$";
String Regex_combination_of_letters_and_specialcharachters = "^(?=.*[a-zA-Z])(?=.*[##$%^&+=])[a-zA-Z##$%^&+=]+$";
String Regex_combination_of_numbers_and_specialcharachters = "^(?=.*[0-9])(?=.*[##$%^&+=])[0-9##$%^&+=]+$";
String Regex_combination_of_letters_and_numbers_and_specialcharachters = "^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[##$%^&+=])[a-zA-Z0-9##$%^&+=]+$";
if(str.matches(Regex_combination_of_letters_and_numbers))
return true;
if(str.matches(Regex_just_letters))
return true;
if(str.matches(Regex_just_numbers))
return true;
if(str.matches(Regex_just_specialcharachters))
return true;
if(str.matches(Regex_combination_of_letters_and_specialcharachters))
return true;
if(str.matches(Regex_combination_of_numbers_and_specialcharachters))
return true;
if(str.matches(Regex_combination_of_letters_and_numbers_and_specialcharachters))
return true;
return false;
}
You can delete some conditions according to your taste