This works:
CopyString 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:
CopyString 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:
Copy^\d+\.\s{1}[A-Z]+.*
(pay attention on trailing .*)
And I'd also use \. instead of [.]. It is more readable.
Validating a string using regex
How to use Java Regex?
Still no split without regex?
Fact: String.split is error prone and slower.
without interpreting its argument as regex
In JDK, String.split actually splits w/o regex if your separator is a one non-regexp char, but it's an implementation detail...
IMHO, all String methods that take regexp String should be deprecated and replaced with a new version that takes explicit java.util.regex.Pattern param, e.g.: String.replaceAll(String, String) -> String.replaceAll(Pattern, String) (or even better: String.replaceAllSecondParamIsNOTWhatYouThink(Pattern, String) - not a joke - replaceAll is often used incorrectly)
Regular expression for parsing suffixes and prefixes?
Videos
Hi,
I have written a code to validate the string using regex by following the code at:
regex code for alphabetical string
My regex code is in the following function:
private void CheckIfInputValid(String input, String regex){
String s; //for(int i=0; i<4; ++i) {
Pattern pattern = Pattern.compile(regex);
s = input.trim(); Matcher matcher = pattern.matcher(s);
JOptionPane.showMessageDialog(null, "Input "+s+" is valid " + String.valueOf(matcher.matches()));//}
}I have also created a form having a text box and a submit button. In the text box I am inputting a string like: “abacde”. The input is a non-numerical text-based string. The code of submit button is:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//TODO add your handling code here:
StringstrAccName= jTF1.getText();
String strInput[] = new String[5];
strInput[0]= strAccName;
String regex = "[a-zA-Z]";
CheckIfInputValid(strInput[0], regex);
}I am getting a false value for matcher.matches().
Somebody please guide me what is the problem with my regex.
I have a problem where I am supposed to get different values from a text file. The problem states: "Use Scanner and an appropriate regular expression."
A line in the text file looks like: "Name=Bob,WaitTime=2990,Rings=3"
I've so far been able to create a scanner and use a while loop(While file.hasNextLine()) to run through the lines of the text file and store them as a String.
I have used the split method to split the string at the ',' character - String[] tokens = inputLine.split(",");
Now what?
How can I get the specific pieces of the string that I want? I.E. how can I parse the name string and store it, how can I parse the waitTime as a long, and how can I parse the rings as an int?
So for this example I'd be able to have: "Bob" 2990 and 3 all parsed and stored in appropriate variables.
Thanks in advance. Edit:Spelling