This works:
String rex = "^\\d+\\.\\s\\p{Lu}+.*";
System.out.println("1. PTYU fmmflksfkslfsm".matches(rex));
// true
System.out.println(". PTYU fmmflksfkslfsm".matches(rex));
// false, missing leading digit
System.out.println("1.PTYU fmmflksfkslfsm".matches(rex));
// false, missing space after .
System.out.println("1. xPTYU fmmflksfkslfsm".matches(rex));
// false, lower case letter before the upper case letters
Breaking it down:
^= Start of string\d+= One or more digits (the\is escaped because it's in a string, hence\\)\.= A literal.(or your original[.]is fine) (again, escaped in the string)\s= One whitespace char (no need for the{1}after it) (I'll stop mentioning the escapes now)\p{Lu}+= One or more upper case letters (using the proper Unicode escape — thank you, tchrist, for pointing this out in your comment below. In English terms, the equivalent would be[A-Z]+).*= Anything else
See the documentation here for details.
You only need the .* at the end if you're using a method like String#match (above) that will try to match the entire string.
This works:
String rex = "^\\d+\\.\\s\\p{Lu}+.*";
System.out.println("1. PTYU fmmflksfkslfsm".matches(rex));
// true
System.out.println(". PTYU fmmflksfkslfsm".matches(rex));
// false, missing leading digit
System.out.println("1.PTYU fmmflksfkslfsm".matches(rex));
// false, missing space after .
System.out.println("1. xPTYU fmmflksfkslfsm".matches(rex));
// false, lower case letter before the upper case letters
Breaking it down:
^= Start of string\d+= One or more digits (the\is escaped because it's in a string, hence\\)\.= A literal.(or your original[.]is fine) (again, escaped in the string)\s= One whitespace char (no need for the{1}after it) (I'll stop mentioning the escapes now)\p{Lu}+= One or more upper case letters (using the proper Unicode escape — thank you, tchrist, for pointing this out in your comment below. In English terms, the equivalent would be[A-Z]+).*= Anything else
See the documentation here for details.
You only need the .* at the end if you're using a method like String#match (above) that will try to match the entire string.
It depends which method are you using. I think it will work if you use Matcher.find(). It will not work if you are using Matcher.matches() because match works on whole line. If you are using matches() fix your pattern as following:
^\d+\.\s{1}[A-Z]+.*
(pay attention on trailing .*)
And I'd also use \. instead of [.]. It is more readable.
Little context im learning regex in class and my teacher keeps saying that we should always use ^ and $ so the matches() method works but it works just fine without it.
Now idk if using both of them is just good practice, meant for something else or that java used to give wrong outputs from said method without using it?
Edit: turns out its not necessary for the matches() method but it is necessary for Matcher class if you want to find exactly the regex youre using inside a text; "\\d{2}" will return false with the method while the find() method inside Matcher will return true if the text has more than 2 numbers