简体   繁体   中英

Regular expression to match string pattern mention

I am using java in which I have to check that input string using regular expression. I am newbie to regular expression.

I am entering input in the form .

Following is sample data for which I need to write down regular expression.

LastName  FirstName

Ab.CD           BC
Ab-CFgD         Ab.CD
F'b-CF gD       BC
F'b-CF gD.      F.b-CF gD'D
Fb-CF gD'D      BC
F.b-CF gD'D     F'b-CF gD

Means It contain dot(.), Hiphen(-), And single quotes(') in between of LastName and First name.

I have wrote down regexx only for "Lastname, first name".

"[a-zA-Z]+, *[a-zA-Z]+"

What would the regular expression be for find all strings mentioned above table

Try this: -

"([a-zA-Z.'-]+),[ ]*([a-zA-Z.'-]+)"

Explanation : -

(            // Capture group 1
  [          // Character class
    a-zA-Z   // alphabets
    .        // a Dot
    '        // A quote
    -        // A hyphen (Should be at the end, in the middle it means a range
  ]+         // Character class close. One or more repetition
)            // Group 1 closes

Seems like there is also a space between your lastnames or firstnames, as given in your example. Please check that if it is correct.

You have two capture groups in the above regex. Both the groups capture firstname and lastname . You can print both of them.

A Sample run over some strings: -

String str1 = "Ab.CD, Ab.CD";
String str2 = "F'b-CFgD., F'b-CFgD.";
String str3 = "F'b-CF gD, F'b-CF gD";

System.out.println(str1.matches("([a-zA-Z.'-]+),[ ]*([a-zA-Z.'-]+)"));  // true
System.out.println(str2.matches("([a-zA-Z.'-]+),[ ]*([a-zA-Z.'-]+)"));  // true
System.out.println(str3.matches("([a-zA-Z.'-]+),[ ]*([a-zA-Z.'-]+)"));   //false

As you can see, last one prints false, as there is a space between your 3rd string. If you want to match space also, then add it to your character class.


To make sure that firstname and lastname start only with letters, you can modify your regex like this: -

"([a-zA-Z][a-zA-Z.'-]+),[ ]*([a-zA-Z][a-zA-Z.'-]+)"

Added [a-zA-Z] at the start of both the patterns, to make sure the first character is from the given alphabet range.

"([a-zA-Z\\\\.\\\\-\\\\']*),\\\\s*([a-zA-Z\\\\.\\\\-\\\\']*)"

Use this web-site to play with regex to figure what will work for you.

Regex Test Site

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM