I am new to matches in java. I want to determine if the last character of a string is a vowel (ie aieou). For example if the string is abcde, then it is ok. But if it is eaoid, then it is wrong.
str.matches(".*[aeiou]$");
.* matches any character zero or more times
[aeiou] matches one of the characters in the set
$ matches the end of the string.
So "abcde".matches(".*[aeiou]$") == true and "eaoid".matches(".*[aeiou]$") == false
The matches() method in java must must the whole string in order to return true, so you need to start the regex with .* and finish it with a character class (square brackets around a list of characters), which is the regex way of saying "one of these characters"
If you want to match strings that end in either an upper or a lower case vowel:
str.matches(".*[AEIOUaeiou]");
or even more simply:
str.matches(".*(?i)[aeiou]");
The regex (?i) means "ignore case"
Related
I have a String " Karren Warren, this is a very good product " and I want to use a regex to return true whenever the first letter in the first word is capitalized and the first word in the second letter is capitalized. Meaning the two words whose first letters are capitalized has to be consecutive.
So in the example given above, the regex would return true because K is capitalized and W is capitalized. Conversely, it would return false in the scenario when the text is Karren warren, Kindly check this out
I used this pattern ([A-Z]\w+\s){2}but it keeps on returning false.
You could use this regex:
^\s*[A-Z][^\s]+\s+[A-Z]
Demo: https://regex101.com/r/B8XDKg/1/
To match all uppercase letters that have a lowercase variant, you could use \p{Lu}. If you don't want to cross newlines, you can use \h to match horizontal whitespace chars, as \s could also match a newline.
^\p{Lu}\S+\h+\p{Lu}
Regex demo | Java demo
In Java with the doubled backslashes
String regex = "^\\p{Lu}\\S+\\h+\\p{Lu}";
I have a string from which I would like to caputre all after and including colon until (excluding) white space or paranthesis.
Why does the following regex include the paranthesis in the string match?
:(.*?)[\(\)\s] or also :(.+?)[\)\s] (non-greedy) does not work.
Example input: WHERE t.operator_id = :operatorID AND (t.merchant_id = :merchantID) AND t.readerApplication_id = :readerApplicationID AND t.accountType in :accountTypes
Should exctract :operatorID, :merchantID, :readerApplicationID, :accountTypes.
But my regexes extract for the second match :marchantID)
What is wrong and why?
Even if I use an exacter mapping condition in the capture, it does not work: :([a-zA-z0-9_]+?)[\)\(\s]
Put your conditional "followed by space or paren" as a lookahead, so that it sees but doesn't match. Right now you are explicitly matching parentheses with [\(\)\s]:
:(.+?)(?=[\s\(\)])
https://regex101.com/r/im8KWF/1/
Or, use the built-in \b "word boundary", which is also a "zero-width" assertion meaning the same thing*:
:(.+?)\b
https://regex101.com/r/FnnzGM/3/
*Definition of word boundary from regular-expressions.info:
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.
How can I make my string only pass a test if every character in the string is in the regex?
Here is what I have so far:
String w = theApplet.Word.getText().toLowerCase();
if(w.matches(".*[a-z-_]+.*")){
theApplet.words.add(w);
theApplet.str.setText("The word: "+w+" has been added to the list");
}
However, the string is valid even if it contains invalid characters, as long as it contains at least 1 of the characters in the regex.
.* means "match any character zero or more times"
[a-z-_]+ means "match any lowercase character or dash (-) or underscore (_) one or more times".
So the first part is consuming nearly the entire string and the regex is returning true if there is at least one lowercase character/dash/underscore.
Simply remove the .*'s to force all characters to be lowercase characters/dashes/underscores.
I have string with *(asterisk) symbols as an input. String is considered as invalid if it has two consecutive asterisks. But, there is an escape symbol \ (backslash).
For example:
"**" (invalid)
"\**" (valid)
"case**" (invalid)
"case\**" (valid)
"*\*" (valid)
I'm on stuck on such regex's which produce incorrect result:
/[^\\]\*\*/ - java.util.regex.Pattern.compile("/[^\\\\]\\*\\*/")
/([^\\]*?\*\*)|(\*\*)/ - java.util.regex.Pattern.compile("/([^\\\\]*?\\*\\*)|(\\*\\*)/").
Also, I've read about greedy, reluctant and possessive quantifies from here http://docs.oracle.com/javase/tutorial/essential/regex/quant.html
I know that problem is about zero-length matches, but could not produce correct regex.
Use string.matches method. This returns true for valid strings.
String s1 = "case**";
String s2 = "case\\**";
System.out.println(s1.matches("(?=.*(\\\\\\*\\*|\\*\\\\\\*)).*"));
System.out.println(s2.matches("(?=.*(\\\\\\*\\*|\\*\\\\\\*)).*"));
Output:
false
true
DEMO
Are you looking for a regex, that will only match invalid strings? This should do:
"(?<!\\\\)\\*\\*+"
It will match two or more asterisks in a row, not preceded by a backslash.
EDIT: (?<!foo) thingy is called "negative look-behind". It matches any zero-length place in the string that is not immediately preceded by a region matching the regex inside parentheses ("foo" in this case, or a backslash in yours).
I had this as [^\\\\] at first, which is almost the same thing (in this case), except that it matches any character, other than a backslash, but not an absense of a character, like at the beginning of a string in "**".
There is a good detailed description of lookarounds (look-behind and look-ahead) as well as a lot of other regex "magic" here
I've already gone through:
Regex to match four repeated letters in a string using a Java pattern
and
Regular expression to match any character being repeated more than 10 times
But they aren't useful in my case. They are fine if I just want to check if a string is containing repeated characters (like 1111, abccccd, 12aaaa3b, etc.). What I want is to check if string is comprising entirely of repeated characters only i.e. aabb111, 1111222, 11222aaa, etc.
Can anyone help me out with this?
Use ((.)\2+)+ as pattern:
String pattern = "((.)\\2+)+";
System.out.println("a".matches(pattern)); // false
System.out.println("1aaa".matches(pattern)); // false
System.out.println("aa".matches(pattern)); // true
System.out.println("aabb111".matches(pattern)); // true
System.out.println("1111222".matches(pattern)); // true
System.out.println("11222aaa".matches(pattern)); // true
System.out.println("etc.".matches(pattern)); // false
About the pattern:
(...): capture matched part as group. (starting from 1)
((.)\2+)+
^^
|+----- group 2
+----- group 1
(.): match any character (except newline) and capture it as group 2 (because it come after enclosing parenthesis).
\2: backreference to the matched group. If (.) matched a character x, \2 matches another x (not any character, but only x).
PATTERN+: matches one or more matches of PATTERN.
(.)\2+: match repeating characters greedy.