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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:23:43+00:00 2026-06-15T19:23:43+00:00

I have a question. Lets say there are such patterns: >>> import re >>>

  • 0

I have a question. Lets say there are such patterns:

>>> import re
>>> pt ='^a{1,2}$'
>>> re.search(pt, 'aa') # looks ok
<_sre.SRE_Match object at 0x020B2288>
>>> re.search(pt, 'aaa') # ok too
>>>

Now lets try to look for match with another pattern:

>>> pt = '^a{1,2}|x$'
>>> re.search(pt, 'a') # this one looks ok
<_sre.SRE_Match object at 0x020B25D0>
>>> re.search(pt, 'aaax') # (1) Now this one?
<_sre.SRE_Match object at 0x020B2288>
>>> re.search(pt, 'aaaaaax') # (2) and this one?
<_sre.SRE_Match object at 0x020B25D0>
>>> re.search(pt, 'aaa') # (3) and this one?
<_sre.SRE_Match object at 0x020B25D0>

(1)(2)(3) To me it looks like it should match string that starts with one or two ‘a’ or one ‘x’ or both combinations and ends between these letters, but nothing else. Or I don’t get it something? Is it should be like that? Like when you use ‘|’, it ignores what limit is put inside {}?
Can someone explain me this?

  • 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-15T19:23:44+00:00Added an answer on June 15, 2026 at 7:23 pm

    The $ is affected by the grouping. Your regex is interpreted as (^a{1,2})|(x$), which matches either “one or two as at the beginning of the string” OR “an x at the end of the string”. If you want to have the | apply only to the as and xs, you need to group them:

    pt = '^(a{1,2}|x)$'
    

    Or, if you don’t want to capture the group, use a noncapturing group:

    pt = '^(?:a{1,2}|x)$'
    

    Edit: I’m not sure I understand what you’re trying to match, but perhaps try:

    pt = '^(a{1,2}x?|x)$"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding events in c#. Lets say I have an object
I have a question regarding foreign key in entity framework(EF4). Lets say i have
This question is for pure curiosity. Lets say I have lighttpd installed on my
My question is i have a certain money amount, lets say 552. I wish
Here is a silly question. Lets say I have a query that produces for
just a question that needs a quick answer, I have a Action, lets say,
Lets say I have a table called ABC in a MS-Access Database. There are
Possible stupid question: let's say I have two apps contained within one project. Can
I have a question about how Hibernate persists entity relations. Let's say I have
I have a question to which I have not found an answer. Let's say

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.