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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:12:40+00:00 2026-05-18T01:12:40+00:00

I’m trying to write a regex that matches xa?b?c? but not x. In reality,

  • 0

I’m trying to write a regex that matches xa?b?c? but not x. In reality, ‘x’, ‘a’, ‘b’, and ‘c’ are not single characters, they are moderately complex sub-expressions, so I’m trying to avoid something like x(abc|ab|ac|bc|a|b|c). Is there a simple way to match “at least one of a, b, and c, in that order” in a regex, or am I out of luck?

  • 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-18T01:12:41+00:00Added an answer on May 18, 2026 at 1:12 am

    Here’s the shortest version:

    (a)?(b)?(c)?(?(1)|(?(2)|(?(3)|(*FAIL))))
    

    If you need to keep around the match in a separate group, write this:

    ((a)?(b)?(c)?)(?(2)|(?(3)|(?(4)|(*FAIL))))
    

    But that isn’t very robust in case a, b, or c contain capture groups. So instead write this:

    (?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL))))
    

    And if you need a group for the whole match, then write this:

    (?<M>(?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL)))))
    

    And if like me you prefer multi-lettered identifiers and also think this sort of thing is insane without being in /x mode, write this:

    (?x)
    (?<Whole_Match>
        (?<Group_A> a) ?
        (?<Group_B> b) ?  
        (?<Group_C> c) ?
    
        (?(<Group_A>)           # Succeed 
          | (?(<Group_B>)       # Succeed
              | (?(<Group_C>)   # Succeed
                  |             (*FAIL)
                )
            )
        )
     )
    

    And here is the full testing program to prove that those all work:

    #!/usr/bin/perl
    use 5.010_000;
    
    my @pats = (
        qr/(a)?(b)?(c)?(?(1)|(?(2)|(?(3)|(*FAIL))))/,
        qr/((a)?(b)?(c)?)(?(2)|(?(3)|(?(4)|(*FAIL))))/,
        qr/(?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL))))/,
        qr/(?<M>(?<A>a)?(?<B>b)?(?<C>c)?(?(<A>)|(?(<B>)|(?(<C>)|(*FAIL)))))/,
        qr{
            (?<Whole_Match>
    
                (?<Group_A> a) ?
                (?<Group_B> b) ?
                (?<Group_C> c) ?
    
                (?(<Group_A>)               # Succeed
                  | (?(<Group_B>)           # Succeed
                      | (?(<Group_C>)       # Succeed
                          |                 (*FAIL)
                        )
                    )
                )
    
            )
        }x,
    );
    
    for my $pat (@pats) {
        say "\nTESTING $pat";
        $_ = "i can match bad crabcatchers from 34 bc and call a cab";
        while (/$pat/g) {
            say "$`<$&>$'";
        }
    }
    

    All five versions produce this output:

    i <c>an match bad crabcatchers from 34 bc and call a cab
    i c<a>n match bad crabcatchers from 34 bc and call a cab
    i can m<a>tch bad crabcatchers from 34 bc and call a cab
    i can mat<c>h bad crabcatchers from 34 bc and call a cab
    i can match <b>ad crabcatchers from 34 bc and call a cab
    i can match b<a>d crabcatchers from 34 bc and call a cab
    i can match bad <c>rabcatchers from 34 bc and call a cab
    i can match bad cr<abc>atchers from 34 bc and call a cab
    i can match bad crabc<a>tchers from 34 bc and call a cab
    i can match bad crabcat<c>hers from 34 bc and call a cab
    i can match bad crabcatchers from 34 <bc> and call a cab
    i can match bad crabcatchers from 34 bc <a>nd call a cab
    i can match bad crabcatchers from 34 bc and <c>all a cab
    i can match bad crabcatchers from 34 bc and c<a>ll a cab
    i can match bad crabcatchers from 34 bc and call <a> cab
    i can match bad crabcatchers from 34 bc and call a <c>ab
    i can match bad crabcatchers from 34 bc and call a c<ab>
    

    Sweet, eh?

    EDIT: For the x in the beginning part, just put whatever x you want at the start of the match, before the very first optional capture group for the a part, so like this:

    x(a)?(b)?(c)?(?(1)|(?(2)|(?(3)|(*FAIL))))
    

    or like this

    (?x)                        # enable non-insane mode
    
    (?<Whole_Match>
        x                       # first match some leader string
    
        # now match a, b, and c, in that order, and each optional
        (?<Group_A> a ) ?
        (?<Group_B> b ) ?  
        (?<Group_C> c ) ?
    
        # now make sure we got at least one of a, b, or c
        (?(<Group_A>)           # SUCCEED!
          | (?(<Group_B>)       # SUCCEED!
              | (?(<Group_C>)   # SUCCEED!
                  |             (*FAIL)
                )
            )
        )
    )
    

    The test sentence was constructed without the x part, so it won’t work for that, but I think I’ve shown how I mean to go at this. Note that all of x, a, b, and c can be arbitrarily complex patterns (yes, even recursive), not merely single letters, and it doesn’t matter if they use numbered capture groups of their own, even.

    If you want to go at this with lookaheads, you can do this:

    (?x)
    
    (?(DEFINE)
           (?<Group_A> a)
           (?<Group_B> b)
           (?<Group_C> c)
    )
    
    x
    
    (?= (?&Group_A)
      | (?&Group_B)
      | (?&Group_C)
    )
    
    (?&Group_A) ?
    (?&Group_B) ?
    (?&Group_C) ?
    

    And here is what to add to the @pats array in the test program to show that this approach also works:

    qr{
        (?(DEFINE)
            (?<Group_A> a)
            (?<Group_B> b)
            (?<Group_C> c)
        )
    
        (?= (?&Group_A)
          | (?&Group_B)
          | (?&Group_C)
        )
    
        (?&Group_A) ?
        (?&Group_B) ?
        (?&Group_C) ?
    }x
    

    You’ll notice please that I still manage never to repeat any of a, b, or c, even with the lookahead technique.

    Do I win? ☺

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.