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

  • Home
  • SEARCH
  • 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 6357985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:16:51+00:00 2026-05-24T23:16:51+00:00

Is it possible to write a regular expression which will match any subset of

  • 0

Is it possible to write a regular expression which will match any subset of a given set of characters
a1 ... an ?
I.e. it should match any string where any of these characters appears at most once, there are no other characters and the relative order of the characters doesn’t matter.

Some approaches that arise at once:
1. [a1,...,an]* or (a1|a2|...|an)*– this allows multiple presence of characters
2. (a1?a2?...an?) – no multiple presence, but relative order is important – this matches any subsequence but not subset.
3. ($|a1|...|an|a1a2|a2a1|...|a1...an|...|an...a1), i.e. write all possible subsequences (just hardcode all matching strings :)) of course, not acceptable.

I also have a guess that it may be theoretically impossible, because during parsing the string we will need to remember which character we have already met before, and as far as I know regular expressions can check out only right-linear languages.

Any help will be appreciated. Thanks in advance.

  • 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-24T23:16:51+00:00Added an answer on May 24, 2026 at 11:16 pm

    Can’t think how to do it with a single regex, but this is one way to do it with n regexes: (I will usr 1 2 … m n etc for your as)

    ^[23..n]*1?[23..n]*$
    ^[13..n]*2?[13..n]*$
    ...
    ^[12..m]*n?[12..m]*$
    

    If all the above match, your string is a strict subset of 12..mn.

    How this works: each line requires the string to consist exactly of:

    • any number of charactersm drawn fromthe set, except a particular one
    • perhaps a particular one
    • any number of charactersm drawn fromthe set, except a particular one

    If this passes when every element in turn is considered as a particular one, we know:

    • there is nothing else in the string except the allowed elements
    • there is at most one of each of the allowed elements

    as required.


    for completeness I should say that I would only do this if I was under orders to “use regex”; if not, I’d track which allowed elements have been seen, and iterate over the characters of the string doing the obvious thing.

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

Sidebar

Related Questions

Is it possible to write a regular expression which checks if a string (some
Possible Duplicate: What regular expression can never match? how do i write a regular
I just want to create a regular expression out of any possible string. var
Is it possible to write a regular expression which matches regular expressions? Does anyone
Is it possible to write code which generates a regular expression or XPath that
Is it possible to write a regular expression that matches a nested pattern that
Is it possible to write a regular expression for finding a typical file matching
Is it possible to write a regular expression to replace everything between <div id=somevalue123
Is it possible to write a regular expression to check whether all numbers of
Possible Duplicate: Regex to match URL I wrote a regular expression to validate only

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.