Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8916839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:21:00+00:00 2026-06-15T05:21:00+00:00

I am using java in which I have to check that input string using

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:21:01+00:00Added an answer on June 15, 2026 at 5:21 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java application which is using a certain Java Runtime Environment. The
I am using java and have to deal with numbers larger than long (which
I have a small application in java which searches images using bing image search.
Suppose, I have a lot of classes, which are constructed using Java reflection (for
I have a Java program which is using WMIC to get certain information about
We have a Java client which is using corba to call several third party
I have a java server which is using TCP and sockets to connect to
I have a Java application which interacts with native code using JNI. The native
I have a java web application which I can debug using IntelliJ or Eclipse
I have a Java handler code which Im using in android to run a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.