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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:07:00+00:00 2026-05-21T18:07:00+00:00

I have a set of n tokens (e.g., a, b, c) distributed among a

  • 0

I have a set of n tokens (e.g., a, b, c) distributed among a bunch of other tokens. I would like to know if all members of my set occur within a given number of positions (window size). It occurred to me that it may be possible to write a RegEx to capture this state, but the exact syntax eludes me.

          11111
012345678901234
ab ab bc  a cba

In this example, given window size=5, I would like to match cba at positions 12-14, and abc in positions 3-7.

Is there a way to do this with RegEx, or is there some other kind of grammar that I can use to capture this logic?

I am hoping to implement this in Java.

  • 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-21T18:07:01+00:00Added an answer on May 21, 2026 at 6:07 pm

    Here’s a regex that matches 5-letter sequences that include all of ‘a’, ‘b’ and ‘c’:

    (?=.{0,4}a)(?=.{0,4}b)(?=.{0,4}c).{5}
    

    So, while basically matching any 5 characters (with .{5}), there are three preconditions the matches have to observe. Each of them requires one of the tokens/letters to be present (up to 4 characters followed by ‘a’, etc.). (?=X) matches “X, with a zero-width positive look-ahead”, where zero-width means that the character position is not moved while matching.

    Doing this with regexes is slow, though.. Here’s a more direct version (seems about 15x faster than using regular expressions):

    public static void find(String haystack, String tokens, int windowLen) {
        char[] tokenChars = tokens.toCharArray();
        int hayLen = haystack.length();
    
        int pos = 0;
        nextPos:
        while (pos + windowLen <= hayLen) {
            for (char c : tokenChars) {
                int i = haystack.indexOf(c, pos);
                if (i < 0) return;
    
                if (i - pos >= windowLen) {
                    pos = i - windowLen + 1;
                    continue nextPos;
                }
            }
    
            // match found at pos
            System.out.println(pos + ".." + (pos + windowLen - 1) + ": " + haystack.substring(pos, pos + windowLen));
            pos++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string with a set of delimited tokens that looks something like
I have set a background on the data-role=page element like so <div data-role=page style=background:
we're going to generate all possible math equations today. Given a a syntax like
I have a large set of data that I've collected that I'd like to
I have an environment which contains a set of tokens that I have already
I have two variables that were being set to session tokens for testing purposes
I have set the eclipse java formatter to wrap lines that exceed 120 characters
I have set up Eclipse Indigo to do line debugging with CFEclipse over port
I have set up upload_max_filesize and post_max_size to 32Mb in php.ini. I am using
I have set of flat files (114 files) each file is named with database

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.