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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:27:26+00:00 2026-06-04T20:27:26+00:00

Having trouble figuring out a regex issue. We are looking for 2 numbers then

  • 0

Having trouble figuring out a regex issue.

We are looking for 2 numbers then hyphen or space then 6 numbers. Must be only 6 numbers, so either an alpha character or some punctuation or space must follow the 6 numbers or the 6 numbers must be at the end of the string.

Other numbers are allowed elsewhere in the string, as long as they are separate.

So, these should match:

foo 12-123456 bar  
12-123456 bar  
foo 12-123456  
foo12-123456bar  
12-123456bar  
foo12-123456  
12-123456bar 99
foo12-123456 99 

These should not match:

123-12345 bar  
foo 12-1234567  
123-12345bar  
foo12-1234567  

Here’s what we were using:

\D\d{2}[-|/\ ]\d{6}\D

and in Expresso this was fine.

But running for real in our .net application this pattern was failing to match on examples where the 6 numbers were at the end of the string.

Tried this:

\D\d{2}[-|/\ ]\d{6}[\D|$]

and it still doesn’t match

foo 12-123456
  • 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-04T20:27:29+00:00Added an answer on June 4, 2026 at 8:27 pm

    I would restate your pattern from

    Must be only 6 numbers, so either an alpha character or some punctuation or space must follow the 6 numbers or the 6 numbers must be at the end of the string.

    to

    Must be only 6 numbers, so there must not be a number after the sixth number

    and then use a negative look-ahead assertion to express this. Similarly, at the start of the pattern use a negative look-behind assertion to say that whatever is before the first two digits, it isn’t a digit. Together:

    var regex = new Regex(@"(?<!\d)\d{2}[- ]\d{6}(?!\d)");
    
    var testCases = new[]
                        {
                            "foo 12-123456 bar",
                            "12-123456 bar",
                            "foo 12-123456",
                            "foo12-123456bar",
                            "12-123456bar",
                            "foo12-123456",
                            "123-12345 bar",
                            "foo 12-1234567",
                            "123-12345bar",
                            "foo12-1234567",
                        };
    
    foreach (var testCase in testCases)
    {
        Console.WriteLine("{0} {1}", regex.IsMatch(testCase), testCase);
    }
    

    This produces six Trues then four Falses, as required.

    The assertions (?<!\d) and (?!\d) respectively say ‘there isn’t a digit just before here’ and ‘there isn’t a digit just after here’.

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

Sidebar

Related Questions

I'm having trouble figuring out how to correctly merge rows. Looking at the sample
I'm having trouble figuring out how to solve this issue. I have a file
I'm having some trouble figuring out why I can only get the children of
I'm having trouble figuring out how to log messages with Sinatra. I'm not looking
I'm having trouble figuring out this issue I'm having with cookies and asp.net applications.
im having trouble figuring out how to bind mouseout() to my entire nav bar
I'm having trouble figuring out how to delete a set of records when a
I'm having trouble figuring out a way to conditionally cancel an asp:TextBox's OnTextChanged AutoPostBack
I'm having trouble figuring out how to create a foreign key constraint. My data
I'm having trouble figuring out how to exclude /public/bin from this rewrite rule RewriteCond

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.