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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:55:24+00:00 2026-06-15T07:55:24+00:00

I am trying to build a regex that should match for example. b abab

  • 0

I am trying to build a regex that should match for example.

b
abab
babab

but not

bb
babb
aaaba
abaaba

At the moment I have a(b)|b(a) and it is working for abab. I am missing the first and last letter, for example b or babab.

So I need to specify a alone or b alone or a letter at the end of a word (if the letter before it isn’t itself). But I can’t figure out how to do that.

I am using http://www.rexv.org/ (Perl PCRE) to try it.

Thanks guys but i forgot to mention:
An empty string can also be matched,
and i can only use the following

* ? +

|

()

.

Thanks guys!,

i suppose it isn’t possible without being able to specify beginning and end of the string to work correctly at http://www.rexv.org/

  • 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-15T07:55:26+00:00Added an answer on June 15, 2026 at 7:55 am

    Try something like this:

    ^((?:(?:ab)*a?)|(?:(?:ba)*b?))$
    

    Explained:

    ^(                   # beginning of the string
        (?:
            (?:ab)*      # matches any repeating `ab` group
            a?           # group can optionally end with an `a`
        )
        |
        (?:
            (?:ba)*      # matches any repeating `ba` group
            b?           # group can optionally end with a `b`
        )
    )$                   # end of the string
    

    I include the subgroups as non-capturing with the leading (?: using a full-capturing group around the entire regex. This will make sure to return you only the full-strings that match instead of the noise of each sub-group.

    The caveat to this approach is that an “empty” string will also match.

    UPDATE (limited set of characters)
    Your limited-set of characters will still work with my pattern above, however, we’ll need to drop the non-matching group portion (?:). The regex will end up as:

    (((ab)*a?)|((ba)*b?))
    

    The caveat mentioned above is that it will also match an empty string, however, this appears to be what you need so we can add that to the bonus-list!

    A slight issue with the set of characters you’re allowed to use is that you aren’t allowed to use the ^ and $ characters which indicate the start and end of a string, respectively. The problem with this is that, any sub-pattern that is matched (regardless of the regex you use) will flag the input as valid. I’m assuming that this is accounted for though.

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

Sidebar

Related Questions

I'm trying build a regex that will replace any characters not of the format:
I'm trying to build a .NET regex to match SQL Server constant strings... but
I've been whacking on this regex for a while, trying to build something that
I'm trying to build a regex somewhat like this: [match-word] ... [exclude-specific-word] ... [match-word]
I'm trying to build a regex that joins numbers in a string when they
I am trying to build a regex function that will remove any non alpha
I am trying to build a regex to match different possible combinations of negative
I'm trying to build a small wiki, but I'm having problems writing the regex
I'm trying to build a regex that can look for names that contain apostrophes
I was trying Build For Archiving application (from Titanium Mobile) with xCode 4.4, but

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.