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 7616965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:01:15+00:00 2026-05-31T03:01:15+00:00

I need help writing a regex with grouping to match the following six input

  • 0

I need help writing a regex with grouping to match the following six input strings:

  1. N.A
  2. N.A.S
  3. N.C.A
  4. N.C.A.S
  5. N.CX.CY.A
  6. N.CX.CY.A.S

The pattern in English is:

(letters) followed by (nothing or letters or letters dot letters) followed by (letters) folowed by (nothing or dot letters)

The result of the regex should be four groups. Given the above examples:

Group 1 is "N" in all cases
Group 2 is empty in case 1-2, "C" for case 3-4, and "CX.CY" for case 5-6
Group 3 is "A" in all cases
Group 4 is "S" in cases 2,4,6 and emtpy in cases 1,3,5

I have played regex whack-a-mole on this and I can get it about half right but when I try to update it for the other cases I end up breaking the ones that used to work.

A solution would be awesome, but hints or tips are equally appreciated.

Update 2012 March 12

As has been pointed out, inputs 2 & 3 look the same (per the English description) and 4 & 5 look the same.

Clarification:

In the real-world input, placeholder ‘S’ has a known value, which is the literal string ‘Value’. In trying to generalize the problem I made it impossible to solve. The updated English description is:

(letters) followed by (nothing or letters or letters dot letters) followed by (letters) folowed by (nothing or “.Value”)

I am attempting to adapt the suggestions below. I had not considered the use of ‘lookaround’, so thanks to everyone who suggested that technique in their examples.

  • 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-05-31T03:01:16+00:00Added an answer on May 31, 2026 at 3:01 am

    Translating your “pattern in English” to regex syntax, this is the best I got:

    ^(\w+)(?:\.(\w+(?:\.\w+)?))?\.(\w+)(?:\.(\w+))?$
    

    Explanation:

    (\w+)          letters
    (?:\.(
       \w+         letters or
       (?:\.\w+)?  letters dot letters or
    ))?            nothing
    
    \.(\w+)        letters
    (?:\.
       (\w+)       letters or
    )?             nothing
    

    It works for cases 1,3,5,6, but for 2,4 the capturing groups are wrong (tested in rubular):

    N.A.S   --> ("N", "A", "S", "")
    N.C.A.S --> ("N", "C.A", "S", "")
    

    Making the first non-capturing group non-greedy fixes the case 2,4 but breaks cases 3,5:

    ^(\w+)(?:\.(\w+(?:\.\w+)??))??\.(\w+)(?:\.(\w+))?$
    
    N.C.A     --> ("N", "", "C", "A")
    N.CX.CY.A --> ("N", "CX", "CY", "A")
    

    I tried a few combinations of greedy/non-greedy groups, but got nothing. IMHO you need to improve your specs for this to be even possible to solve at all…

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

Sidebar

Related Questions

I need some help writing a regex. I have the following strings, xxx.yyy.wwwwwaaa_IN_123 xxx.rrrttttt_IN_12355
I need help writing a regex for a phone number pattern that allows the
being a regex beginner, I need some help writing a regex. It should match
Hi can someone please help me in writing this regex. I need to search
I need help writing regular experssion that will match phone numbers in some data.
I need help in writing a RegEx able to detect 1 or more instance
Need a little help with a regex I'm trying to match a string that
I need help writing a simple regex for RewriteRule for mod_rewrite in htaccess. So,
I need to help about writing regex for below string. I have tried lots
Need help finding or having a RegEx match a MM/YY or MM/YYYY format. My

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.