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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:37:13+00:00 2026-05-15T22:37:13+00:00

I have a string on which I try to create a regex mask that

  • 0

I have a string on which I try to create a regex mask that will show N number of words, given an offset. Let’s say I have the following string:

"The quick, brown fox jumps over the lazy dog."

I want to show 3 words at the time:

offset 0: "The quick, brown"
offset 1: "quick, brown fox"
offset 2: "brown fox jumps"
offset 3: "fox jumps over"
offset 4: "jumps over the"
offset 5: "over the lazy"
offset 6: "the lazy dog."

I’m using Python and I’ve been using the following simple regex to detect 3 words:

>>> import re
>>> s = "The quick, brown fox jumps over the lazy dog."
>>> re.search(r'(\w+\W*){3}', s).group()
'The quick, brown '

But I can’t figure out how to have a kind of mask to show the next 3 words and not the beginning ones. I need to keep punctuation.

  • 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-15T22:37:14+00:00Added an answer on May 15, 2026 at 10:37 pm

    The prefix-matching option

    You can make this work by having a variable-prefix regex to skip the first offset words, and capturing the word triplet into a group.

    So something like this:

    import re
    s = "The quick, brown fox jumps over the lazy dog."
    
    print re.search(r'(?:\w+\W*){0}((?:\w+\W*){3})', s).group(1)
    # The quick, brown 
    print re.search(r'(?:\w+\W*){1}((?:\w+\W*){3})', s).group(1)
    # quick, brown fox      
    print re.search(r'(?:\w+\W*){2}((?:\w+\W*){3})', s).group(1)
    # brown fox jumps 
    

    Let’s take a look at the pattern:

     _"word"_      _"word"_
    /        \    /        \
    (?:\w+\W*){2}((?:\w+\W*){3})
                 \_____________/
                    group 1
    

    This does what it says: match 2 words, then capturing into group 1, match 3 words.

    The (?:...) constructs are used for grouping for the repetition, but they’re non-capturing.

    References

    • regular-expressions.info/Capturing Groups, Non-capturing Groups
      • Repeating a Capturing Group vs Capturing a Repeated Group

    Note on “word” pattern

    It should be said that \w+\W* is a poor choice for a “word” pattern, as exhibited by the following example:

    import re
    s = "nothing"
    print re.search(r'(\w+\W*){3}', s).group()
    # nothing
    

    There are no 3 words, but the regex was able to match anyway, because \W* allows for an empty string match.

    Perhaps a better pattern is something like:

    \w+(?:\W+|$)
    

    That is, a \w+ that is followed by either a \W+ or the end of the string $.


    The capturing lookahead option

    As suggested by Kobi in a comment, this option is simpler in that you only have one static pattern. It uses findall to capture all matches (see on ideone.com):

    import re
    s = "The quick, brown fox jumps over the lazy dog."
    
    triplets = re.findall(r"\b(?=((?:\w+(?:\W+|$)){3}))", s)
    
    print triplets
    # ['The quick, brown ', 'quick, brown fox ', 'brown fox jumps ',
    #  'fox jumps over ', 'jumps over the ', 'over the lazy ', 'the lazy dog.']
    
    print triplets[3]
    # fox jumps over 
    

    How this works is that it matches on zero-width word boundary \b, using lookahead to capture 3 “words” in group 1.

        ______lookahead______
       /      ___"word"__    \
      /      /           \    \
    \b(?=((?:\w+(?:\W+|$)){3}))
         \___________________/
               group 1
    

    References

    • regular-expressions.info/Lookarounds
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.