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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:01:11+00:00 2026-06-11T05:01:11+00:00

I think pat1 = ‘[ab]’ and pat2 = ‘a|b’ have the same function in

  • 0

I think pat1 = ‘[ab]’ and pat2 = ‘a|b’ have the same function in Python(python2.7, windows) ‘re’ module as a regular expression pattern. But I am confused with ‘[ab]+’ and ‘(a|b)+’, do they have the same function, if not can you explain details.

'''
Created on 2012-9-4

@author: melo
'''

import re
pat1 = '(a|b)+'
pat2 = '[ab]+'
text = '22ababbbaa33aaa44b55bb66abaa77babab88'

m1 = re.search(pat1, text)
m2 = re.search(pat2, text)
print 'search with pat1:', m1.group()
print 'search with pat2:', m2.group()

m11 = re.split(pat1, text)
m22 = re.split(pat2, text)
print 'split with pat1:', m11
print 'split with pat2:', m22

m111 = re.findall(pat1, text)
m222 = re.findall(pat2, text)
print 'findall with pat1:', m111
print 'findall with pat2:', m222

output as below:

search with pat1: ababbbaa
search with pat2: ababbbaa
split with pat1: ['22', 'a', '33', 'a', '44', 'b', '55', 'b', '66', 'a', '77', 'b', '88']
split with pat2: ['22', '33', '44', '55', '66', '77', '88']
findall with pat1: ['a', 'a', 'b', 'b', 'a', 'b']
findall with pat2: ['ababbbaa', 'aaa', 'b', 'bb', 'abaa', 'babab']

why are ‘pat1’ and ‘pat2’ different and what’s their difference?
what kind of strings can ‘pat1’ actually match?

  • 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-11T05:01:12+00:00Added an answer on June 11, 2026 at 5:01 am

    You have a capturing group in the first pattern.

    According to the docs,

    re.split()
    … If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. …

    Try making the group non-capturing and see if you get what you expect:

    pat1 = '(?:a|b)+'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Think at this scenario: I have a c# windows form application. This application was
Think an iPhone-based email client. You have three 'states' with the same UI: New
Think Design: I have many applications that share the same user database! Other tables
Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
Think my problem is I am trying to sum a count in the same
I think I have a basic understanding of this, but am hoping that someone
I think this could be a very easy question for you. But I have
Think Windows Explorer's 'Details' view. I need to output my database to this view.
I think I have found a bug when setting or getting the Me.Top property
I think firefox's support to box model in CSS3 is soooooooooooooo poor... I have

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.