Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regex for a string

Tags:

java

string

regex

I would like to generate a regex for a string like "S67-90". I used a syntax "String pattern = "\w\d\d\W\d\d"", but I would like to specify that the first word character should always start wit "S". Can anybody please help me with this?

My sample code is :

                      String pattern = "\\w\\d\\d\\W\\d\\d";
                      Pattern p = Pattern.compile((pattern));
                      Matcher m = p.matcher(result);
                      if (m.find()) {
                      System.out.println("Yes!It is!");
                       }
                     else{
                     System.out.println("No!Its not  :(");
                         }
like image 311
dmurali Avatar asked Sep 26 '26 23:09

dmurali


1 Answers

Just replace first \\w with S in your pattern.

  String pattern = "S\\d\\d\\W\\d\\d";
like image 56
Chuck Norris Avatar answered Sep 28 '26 12:09

Chuck Norris