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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:45:07+00:00 2026-06-15T11:45:07+00:00

I want to find all 2 word strings in python. I created this: #!/usr/bin/python

  • 0

I want to find all 2 word strings in python.
I created this:

#!/usr/bin/python
import re

string='a1 a2 a3 a5 a6'
search=re.findall('.. ..',string)
print len(search)
for nk in search:
        print nk

I am getting:
a1 a2
a3 a5
While I wanted:a1 a2,a2 a3,a3 a5,… etc
The findall should search for all possible patterns?
And why returns a1 a2,a3 a5?
Thank you.

  • 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-15T11:45:08+00:00Added an answer on June 15, 2026 at 11:45 am

    It returns ['a1 a2', 'a3 a5'], because these are the only patterns which can be found: after applying the first one, the 'a1 a2' part is gone and ' a3 a5 a6' is left. The next possible pattern is 'a3 a5', and ' a6' is left over and cannot be matched further.

    'a1 a3', 'a1 a5' etc. cannot be found because this combinations don’t occur. Remember, you search for two arbitrary characters, followed by a space character, followed by 2 arbitrary characters.

    With

    r=re.compile(r"(\S{2})(?:\s|$)")
    pairs =r.findall("a1 a2 a3 a5 a6")
    

    or

    pairs = re.findall(r"(\S{2})(?:\s|$)", "a1 a2 a3 a5 a6")
    

    you find all 2-character combination which are wither followed by a space or by the end of the string: ['a1', 'a2', 'a3', 'a5', 'a6']. If you combine these, you will find all possible combinations:

    for ifirst in range(len(pairs) - 1):
        for second in pairs[ifirst + 1:]:
            print " ".join((pairs[ifirst], second))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a word W, I want to find all words containing the letters in
I want to find all stylesheet definitions in a XHTML file with lxml.etree.findall .
I want to find all img tags in a string of text and put
I want to find all strings containing at least 1 Cyrillic character (basically /.*[А-я].*/)
I want to find string similarity between two strings. en.wikipedia has examples of some
I want to find all the numbers in a string and add 50 to
I want find all Saturdays and Sundays in A given month. How can I
I want to find all the demo words using PHP and regEx. $input ='demo';
I want to find all foreign keys in a table, and for each foreign
I want to find all items in one collection that do not match another

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.