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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:27:27+00:00 2026-06-18T03:27:27+00:00

How do I match an expression where I need to do an or of

  • 0

How do I match an expression where I need to do an or of another set?

i.e., how do I match something of the format

[
  [
    [ a | b ] |
    [ x | y ]
  ]
]

where a, b, x and y are strings.

I want to match the phrases like

a
b
x
y
a x
a y
b x
b y
x a
x b
y a
y b

But not the ones like:

a b
x y
z z 

I’m trying to use it in Boost Xpressive so I have the option to use either ECMAScript or Perl type regular expressions.

  • 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-18T03:27:28+00:00Added an answer on June 18, 2026 at 3:27 am

    You can do it like this:

    [ab] [xy]|[xy] [ab]|[abxy]
    

    There are 3 choices here:

    • Only a, b, x, y (single character)
    • Or 2 character, a or b comes before x or y, space in between.
    • Or 2 character, x or y comes before a or b, space in between.

    I put [abxy] behind, just in case when you search, it will search for those in front (the paired up ones) before searching for the single ones. The order is important if you use the regex to search, but it doesn’t matter much when you do validation.

    Another way to write it:

    [ab]( [xy])?|[xy]( [ab])?
    

    That only works for character, but you can make it works for string. For example, let’s say you have 4 strings s1, s2, s3, s4:

    (s1|s2)( (s3|s4))?|(s3|s4)( (s1|s2))?
    

    It searches for:

    • Either s1 or s2, may or may not (0 or 1 instance of) followed by s3 or s4
    • (The other way around)

    This covers all the cases of s1, s2, etc. (single string), s2 s3, s3 s2, etc. (paired up, can reverse the order). The regex above will search for the longer version (paired up) before resorting to the single string, due to the default greedy property of quantifiers.

    Note that I am using capturing groups (pattern) in the regex above, which will record the position of the string that matches the pattern inside. You can make them non-capturing group (?:pattern), if you don’t need to refer to the text that matches the pattern. This will save you some clock cycles.

    (?:s1|s2)(?: (?:s3|s4))?|(?:s3|s4)(?: (?:s1|s2))?
    

    (I leave the task of changing capturing group to non-capturing group for the other regex as an exercise. It is as simple as adding ?:)

    Searching or Validation?

    If you want to find such pattern, then the regex above should work for you.

    If you want to validate that the string matches the pattern, you need to use anchors ^ (match beginning of string), $ (match the end of the string) to make sure the string follows the exact format:

    ^([ab] [xy]|[xy] [ab]|[abxy])$
    ^([ab]( [xy])?|[xy]( [ab])?)$
    ^((s1|s2)( (s3|s4))?|(s3|s4)( (s1|s2))?)$
    ^(?:(?:s1|s2)(?: (?:s3|s4))?|(?:s3|s4)(?: (?:s1|s2))?)$
    

    Note that I surround the regex from the above sections with () (capturing group, but I only need grouping here actually). This is because I have an alternation | inside.

    Extensibility and Limitations

    • You can add more strings to either the first group or second group as you like:

      ^([abcd]( [xyz])?|[xyz]( [abcd])?)$
      
    • However, if you want to increase the number of groups, I suggest that you split the string by spaces and loop through the tokens to find the permutations of the group, rather than using regex.

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

Sidebar

Related Questions

I am trying to match an expression of type field:value but not field:value I
I need a regular expression able to match everything but a string starting with
Hello I need help with Regular Expression, I want to match each section (number
I need regular expression to match strings, which begin from number (number can be
Hi I need regular expression to match the following format in c# {productName}-{version}-{X}-of-{Y}.EXE Example
Possible Duplicate: Regular expression: match all words except I need your help for using
I need a regular expression that will match the first letter of a song
I want a regular expression to match valid input into a Tags input field
I need to parse a string like func1(arg1, arg2); func2(arg3, arg4); . It's not
In a regular expression, I need to know how to match one thing or

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.