I try to write regex in order to validate that :
not lowercase characters
not only whitespace
I have write a regex (^[^a-z]+$)(^[^ ]+$) but when i test it, SFSFSDis incorrect.
How can i do that ?
Thanks ! :)
You can also use the regex (^(?!\\s+$)[^a-z]+)
(?!\\s+$) part checks if the regex does not contain only whitespaces till the end
[^a-z]+ then check for the lowercase characters
Tested with a few samples:
List<String> wordList = Arrays.asList(" ", "SFSFSD", "as DDdkj", "AB CD", " k", "l l");
String regex = "(^(?!\\s+$)[^a-z]+)";
for(String word : wordList) {
if(word.matches(regex)) {
System.out.println(word + " :valid");
} else {
System.out.println(word + " :not valid");
}
}
Output:
:not valid
SFSFSD :valid
as DDdkj :not valid
AB CD :valid
k :not valid
l l :not valid
You could use:
^[^a-z]*[^a-z\s][^a-z]*$
This would assert that there be at least one non whitespace, non lowercase letter, character, with the remaining being either whitespace or non lowercase.
I use the regex to validate that:
/^(?!^ +$)([^a-z]+)$/
Explanation
^ matches the position before the first character in the string.
(?!^ +$) negative lookahead, that matches if the string not only whitespace.
([^a-z]+) matches all characters except lowercase characters.
$ matches the right after character position of the string.
Related
I'm trying to write a regular expression that matches all the following given pattern examples -
1) Name=John!Age=25!Gender=M
2) Name=John!Name2=Sam!Name3=Josh
3) Name=John!Name2=Sam!Name3=Josh!
Basically there has to be an equals to sign between two words or numbers followed by an exclamatory and then the pattern repeats. the pattern can only end with an exclamatory or any alphabets or numbers or spaces but not the 'equals' sign or any other special characters
So these examples should not be matched -
1) Name!John=Name2+Sam=
2) Name=John=
3) Name=John!!
4) Name=John-
I'm very new to regular expressions and I just learnt a few basic things and I have this following regular expression written so far which doesn't fully satisfy my requirement ((?:\w+|=)*)!
I'm still trying to modify my regular expression to match my requirement, any help/guidance will be very helpful.
You can use
^(?:\w+=\w+!)*\w+=\w+!?$
In Java
String regex = "^(?:\\w+=\\w+!)*\\w+=\\w+!?$";
The pattern matches:
^ Start of string
(?:\w+=\w+!)* Optionally repeat 1+ word chars = 1+ word chars and !
\w+=\w+!? Match 1+ word chars = 1+ word chars and optional !
$ End of string
Regex demo
String[] strings = {
"Name=John!Age=25!Gender=M",
"Name=John!Name2=Sam!Name3=Josh",
"Name=John!Name2=Sam!Name3=Josh!",
"N=J!A=25!",
"a=ba=b",
"Name!John=Name2+Sam=",
"Name=John=",
"Name=John!!",
"Name=John-"
};
for (String s : strings) {
if (s.matches("(?:\\w+=\\w+!)*\\w+=\\w+!?")) {
System.out.println("Match: " + s);
} else {
System.out.println("No match: " + s);
}
}
Output
Match: Name=John!Age=25!Gender=M
Match: Name=John!Name2=Sam!Name3=Josh
Match: Name=John!Name2=Sam!Name3=Josh!
Match: N=J!A=25!
No match: a=ba=b
No match: Name!John=Name2+Sam=
No match: Name=John=
No match: Name=John!!
No match: Name=John-
We are looking to match a registration number of specific pattern and length For example the expression should match (three alphabet - 3 digit)
aaa-080
ccs-124
kfz-213
and reject
knv-2213
asdf-122
Use [a-zA-Z] for alphabet, use \\d for digit, and {3} for occurrence:
System.out.println("xxx-123".matches("^[a-zA-Z]{3}\\-\\d{3}$")); // true
You can try this
^[a-zA-Z]{3}\-\d{3}$
String regex = "^[a-zA-Z]{3}\\-\\d{3}$";
System.out.println("aaa-080".matches(regex));//true
System.out.println("aaa-0800".matches(regex));//false
System.out.println("aaaa-080".matches(regex));//false
Try This
//Your Input "aaa-1234 ccs-123 kfz-123"
System.out.println(Pattern.matches("aaa-[0-9]{3} ccs-[0-9]{3} kfz-[0-9]{3}", "aaa-1234 ccs-123 kfz-123"));
//Your Input "aaa-123 ccs-123 kfz-123"
System.out.println(Pattern.matches("aaa-[0-9]{3} ccs-[0-9]{3} kfz-[0-9]{3}", "aaa-123 ccs-123 kfz-123"));
//Reject
boolean right=Pattern.matches("aaa-[0-9]{3} ccs-[0-9]{3} kfz-[0-9]{3}", "knv-2213 asdf-122");
if(right)
{
System.out.println("Pattern Match");
}
else
{
System.out.println("Pattern Invalid");
}
Output :
false
true
Pattern Invalid
Try this pattren:
^[\w]{3}[-][\d]{3}$
^ : start
[\w]{3} : 3 alphabetical characters (small and capital letters), If you want to match only small characters [a-z]{3}, to become the pattern ^[a-z]{3}[-][\d]{3}$
[-] : -
[\d]{3}$ : ends with 3 numbers
You can test it online
I have regexp for check if some text containing word (with ignoring boundary)
String regexp = ".*\\bSOME_WORD_HERE\\b.*";
but this regexp return false when "SOME_WORD" starts with # (hashtag).
Example, without #
String text = "some text and test word";
String matchingWord = "test";
boolean contains = text.matches(".*\\b" + matchingWord + "\\b.*");
// now contains == true;
But with hashtag `contains` was false. Example:
text = "some text and #test word";
matchingWord = "#test";
contains = text.matches(".*\\b" + matchingWord + "\\b.*");
//contains == fasle; but I expect true
The \b# pattern matches a # that is preceded with a word character: a letter, digit or underscore.
If you need to match # that is not preceded with a word char, use a negative lookbehind (?<!\w). Similarly, to make sure the trailing \b matches if a non-word char is there, use (?!\w) negative lookahead:
text.matches("(?s).*(?<!\\w)" + matchingWord + "(?!\\w).*");
Using Pattern.quote(matchingWord) is a good idea if your matchingWord can contain special regex metacharacters.
Alternatively, if you plan to match your search words in between whitespace or start/end of string, you can use (?<!\S) as the initial boundary and (?!\S) as the trailing one
text.matches("(?s).*(?<!\\S)" + matchingWord + "(?!\\S).*");
And one more thing: the .* in the .matches is not the best regex solution. A regex like "(?<!\\S)" + matchingWord + "(?!\\S)" with Matcher#find() will be processed in a much more optimized way, but you will need to initialize the Matcher object for that.
If you are looking for words with leading '#', just simple remove the leading '#' from the searchword and use following regex.
text.matches("#\\b" + matchingWordWithoutLeadingHash + "\\b");
I use following g to determine if word appears in a text, enforcing word boundaries:
if ( Pattern.matches(".*\\b" + key + "\\b.*", text) ) {
//matched
}
This would match book on text-book but not on facebook.
Now, I would like to to do the reverse: determine if the input text has a word boundary inside.
E.g. mutually-collaborative (CORRECT, there is a word boundary inside) and mutuallycollaborative (WRONG, as there is no word boundary inside).
If the boundary was a punctuation this will work:
if( Pattern.matches("\\p{Punct}", text) ) { //check punctuations
//has punctuation
}
I would like to check for word boundaries in general , e.g. '-', etc.
Any idea?
You want to check if a given string contains a word boundary inside the string. Note that \b matches at the beginning and end of a non-empty string. Thus, you need to exclude those alternatives. Just use
"(?U)(?:\\W\\w|\\w\\W)"
This way, you will make sure a string contains a combination of a word and a non-word characters.
See IDEONE demo:
String s = "mutuallyexclusive";
Pattern pattern = Pattern.compile("(?U)(?:\\W\\w|\\w\\W)");
Matcher matcher = pattern.matcher(s);
if (matcher.find()){
System.out.println(matcher.group() + " word boundary found!");
} else {
System.out.println("Word boundary NOT found in " + s);
}
Just some reference on what a word boundary can match:
There are three different positions that qualify as word boundaries:
Before the first character in the string, if the first character is a word character.
After the last character in the string, if the last character is a word character.
Between two characters in the string, where one is a word character and the other is not a word character.
So, with \w\W|\W\w, we exclude the first 2 situations.
I need a regular expression to match line beginning with a specific WORD, followed by zero or more digits, then nothing more. So far I've tried this:
^WORD\d{0,}
and this:
^WORD[0-9]*
But it doesn't work as expected: it is also matching lines like WORD11a, which I don't want.
I forgot the $ end of line character, so it matched:
WORD1
WORD11
WORD11a
this works, just fine:
^WORD\\d*$
The problem is probably that ^ matches the beginning of the input (I suspect you only find a match if the first line matches), and not the beginning of a line.
You could try using a positive lookbehind saying that the match should be preceded by either start of input (^) or a new line (\n):
String input = "hello156\n"+
"world\n" +
"hello\n" +
"hell55\n";
Pattern p = Pattern.compile("(?<=^|\n)hello\\d*");
Matcher m = p.matcher(input);
while (m.find())
System.out.println("\"" + m.group() + "\"");
Prints:
"hello156"
"hello"
"(\\AWORD[\\d]*$)"
this should do the trick. beginning of input, your WORD, and a number