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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:45:46+00:00 2026-06-11T14:45:46+00:00

The problem I’m trying to create a regex in which we can check if

  • 0

The problem

I’m trying to create a regex in which we can check if all letters present in some reference set are present in some other string, but only in odd numbers (1, 3, 5, …).

Here is a (very) crude image of the DFA representing the problem:

Odd As and Bs DFA

My (broken) solution

I started using a finite set, {a, b}, so I would essentially check “are there both an odd number of as and an odd number of bs in the string?”

Unfortunately I did not get far on my own. I first read this thread, which is remarkably similar to this concept, but was not able to glean an answer from (aa|bb|(ab|ba)(aa|bb)*(ba|ab))*(b|(ab|ba)(bb|aa)*a). (I understand how it works, but not how to convert it to check odd numbers for both items present.)

Here is what I’ve come up with so far: ^((ab|ba)(bb|aa)?|(bb|aa)?(ab|ba))+$. This basically checks if there is ab or ba followed by bb or aa or nothing, which would result in ab, ba, abaa, abbb, baaa, or babb. (It also does the reverse of this, checking the double-letter first.) This can then repeat, indefinitely. The problem I have is I cannot seem to adjust it to match the string bbaaba without also matching bbaa.

Additionally, the method above can not be dynamically adjusted to account for {a, b, c}, for example, though I’m willing to forgo this to solve the initial problem.

Testing

Here are my test strings and the desired output, with the reasons in parentheses:

"ba"      # True (1a, 1b)
"abbb"    # True (1a, 3b)
"bbba"    # True (1a, 3b)
"bbab"    # True (1a, 3b)
"ababab"  # True (3a, 3b)
"bbaaba"  # True (3a, 3b)
"abb"     # False (2b)
"aabb"    # False (2a, 2b)
"aabba"   # False (2b)
""        # False (0a, 0b is "even")
"a"       # False (0b is "even")
"b"       # False (0a is "even")

Question

So, is this possible through regex? Or are regular expressions more limited than a DFA? I am aware that it can be done through a basic loop, but this isn’t what I’m going for.

  • 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-11T14:45:48+00:00Added an answer on June 11, 2026 at 2:45 pm

    Here’s one way to do it, using lookaheads to assert each condition in turn.

    ^(?=[^a]*a(?:[^a]*a[^a]*a)*[^a]*$)(?=[^b]*b(?:[^b]*b[^b]*b)*[^b]*$)(.*)$
    

    Here’s a demo with your examples. (The \ns in the demo are for presentation purposes. Also, you can drop the (.*)$ if you only need to test the match, not capture.)

    I will be adding an explanation shortly.


    Explanation

    We only need to look at one half:

    (?=  [^a]*a  (?:[^a]*a[^a]*a)  *  [^a]*$  )
    |    |       |                 |  |
    |    |       |                 |  Only accept non-'a's to the end.
    |    |       |                 |
    |    |       |                 Zero or more of these pairs of 'a's.
    |    |       |
    |    |       Strictly a pair of 'a's.
    |    |
    |    Find the first 'a'.
    |
    Use a lookahead to assert multiple conditions.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I can set the exposureMode property of AVCaptureDevice, but it does not stay
PRoblem: i'm trying to create (just for fun) a simple poker card (with a
Problem: Trying to create a Mix that is applied to the AVPlayerItem, but it
Problem: I am trying to build a recursive tree using a function and data
Problem Description: We have a service which has applications for main mobile OS’s. We
?Problem is pretty simple, I extend Button to create ImageButton, and I extend ButtonSkin
Problem Description: Multi-player Game that works over the some kind of network, in game
Problem: In MS Access you can hold the shift key when opening a database
Problem I have a file which contains such text (nothing else) 2010-10-05 I would
Problem I'm currently stuck trying to figure out what iPhone OS Deployment Target setting

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.