Regex to check if string contains only one uppercase - java

What is the regex to make sure that a given string contains exactly one uppercase letter? if the string contains more than one i dont want it to be matched.
just 1 Uppercase character
I know the patterns for individual sets namely [a-z], [A-Z], \d and _|[^\w] (I got them correct, didn't I?).
But how do I make them to match only with strings (in java) that only contains 1 uppercase?

You may use this regex with 2 negated character classes:
^[^A-Z]*[A-Z][^A-Z]*$
Above regex will support ASCII upper case letters only. If you want to match unicode letters then use:
^\P{Lu}*\p{Lu}\P{Lu}*$
RegEx Demo 2
RegEx Demo
Here:
\P{Lu}*: Match 0 or more non-uppercase unicode letters
\p{Lu}: Match an uppercase unicode letter

Related

How to check if substring is contained within word Java

I want to write a regex pattern that looks at a string to see if there is a "." followed by letters or numbers or both with no space in between.
Currently I have:
Pattern.matches(".*(\\W+|\\d+|[a-z]+)\\.[a-z]+", testStr)
But this doesn't work if there are numbers or symbols after the "." Can someone help me find a regex string that will return true for the string:
asdad-asdd/asdcs.pd(210)fsd
Just to reiterate the criteria for a successful match is the string contains any possible combination of letters, numbers, and/or symbols before and after a "."
You can match these strings by replacing [a-z]+ with [a-z\d\p{Punct}]+:
Pattern.matches(".*(\\W+|\\d+|[a-z]+)\\.[a-z\\d\\p{Punct}]+", testStr)
The [a-z\d\p{Punct}]+ pattern matches lowercase ASCII letters, digits or punctuation. Add A-Z into the brackets if you plan to allow uppercase ASCII letters. See the regex demo.
However, you might also match any non-whitespace chars with \S+:
Pattern.matches(".*(\\W+|\\d+|[a-z]+)\\.\\S+", testStr)
If you do not want to allow another dot:
Pattern.matches(".*(\\W+|\\d+|[a-z]+)\\.[^\\s.]+", testStr)
Here, [^\\s.]+ matches one or more chars other than whitespace and . chars.

How to match strings with regex to know of the first two words of a letter has their first later capitalised

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}";

Validate a string that can contain any characters, but letters from a specific alphabet/script

I have this string
String s = "Some text, some text!"
I need check string, and if this string have character from other language, like Hebrew or Russian then return false, otherwise if string have only english char(optional with spaces and punct) return true. Of cource string like this String s = ", , ." must return false.
I was try this code
Pattern pEng = Pattern.compile("\\p{Alpha}+\\p{Space}*\\p{Punct}*\\p{Digit}*");
pEng.matcher(s).matches()
but its return false
What i do wrong? Already spend many time for find answer, who can help?
To match a string that only contains ASCII chars and has at least one ASCII letter, you may use
s.matches("[\\p{ASCII}&&[^A-Za-z]]*[A-Za-z]\\p{ASCII}*")
See this Java demo
If you do not want to allow control chars in the input, use a variation of the pattern:
s.matches("[ -~&&[^A-Za-z]]*[A-Za-z][ -~]*")
See this Java demo.
Note that .matches requires a full string match, hence, there is no need adding ^ and $ / \z anchors around the pattern.
Pattern details
[ -~&&[^A-Za-z]]* - 0 or more printable ASCII chars except ASCII letters (&&[^...] is a character class subtraction, it is here to make the pattern work faster, more efficiently)
[A-Za-z] - an ASCII letter (=\p{Alpha})
[ -~]* - 0 or more printable ASCII chars.
The \p{ASCII} Unicode property class matches any ASCII chars.
Additional info
If you need to match a string with only certain script/alphabet letters and any other chars in a string, you may use
s.matches("\\P{L}*(?:[A-Za-z]\\P{L}*)+")
This [A-Za-z] is for English, for Russian, you would use [а-яА-ЯёЁ].
Now, say you want to only match a string whose letters can only be Hebrew letters inside. Since \p{InHebrew} contains all Hebrew script, not just letters, you would use an intersection of this class and a letter \p{L} class, [\p{InHebrew}&&[\p{L}]]:
str.matches("\\P{L}*(?:[\\p{InHebrew}&&[\\p{L}]]\\P{L}*)+")
^^^^^^^^^^^^^^^^^^^^^^^^^

Regex first character must be alpha if more than one character can include numbers and underscore up to 128

As the long title suggests if the test string has only character it can only be alpha. But if the test string has more than one character up to 128 characters then it still must start with an alpha character but then allow numbers and underscores.
This is the regex I have so far. ^([a-zA-Z])|([a-zA-Z][A-Za-z0-9\_]{2,128})$
where it fails is if the second character is an underscore.
Here is the link: https://regex101.com/r/xzmfRs/1
You can use this regex for your problem:
^[a-zA-Z]\w{0,127}$
To allow one alphabet or else allow upto 128 characters of word characters.
\w is shorthand for [a-zA-Z0-9_]
Updated RegEx Demo
This pattern is the matches (^[a-zA-Z]$)|^([a-zA-Z][A-Za-z0-9\_]{2,128})$

Replace last word in a string if it is 2 characters long using regex

I am trying to replace last word of a string if it is 2 characters long using regex. I used [a-zA-Z]{2}$ but it is finding last 2 characters of string. I don't want to replace the last word if it is not exactly 2 characters long, how can I do it?
You need to match a word boundary (\b) before the two letters:
\b[a-zA-Z]{2}$
This will match any two Latin letters that appear at the end of a string, as long as they are not preceded by a 'word' character (which is a Latin letter, digit, or underscore).
In case you want to replace the word even if it is preceded by a digit or underscore, you might want to use a lookbehind assertion, like this:
(?<![a-zA-Z])[a-zA-Z]{2}$
\\b\\w\\w\\b$ (regex in java flavor)
should work as well
Edit: in fact \\b\\w\\w$ should be enough. (or \b\w\w$ in non-java flavor.. see demo link)
You could also use:
[^\p{Alpha}]\p{Alpha}{2}$
Use Alnum instead if digits count as words. This does, however, fail if the entire string is only two characters long.

Categories