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: –
Explanation : –
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 groupsin the above regex. Both the groups capturefirstnameandlastname. You can print both of them.A Sample run over some strings: –
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
firstnameandlastnamestart only with letters, you can modify your regex like this: –Added
[a-zA-Z]at the start of both the patterns, to make sure the first character is from the given alphabet range.