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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:21:19+00:00 2026-05-25T20:21:19+00:00

I have the below regular expression in Python, ^1?$|^(11+?)\1+$ Since there is a pipe

  • 0

I have the below regular expression in Python,

^1?$|^(11+?)\1+$

Since there is a pipe ‘|’, I will split it into 2 regex,

^1?$

For this, it should validate 1 or empty value. Am I correct?

^(11+?)\1+$

For the above regex, it would validate value of 1111. The first pair of 11 is based on (11+?) and the second pair of 11 is due to \1.

When I attempt to execute it in Python, it returns true only for 1111 but not 11 or empty value. Am I wrong somewhere?

  • 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-25T20:21:20+00:00Added an answer on May 25, 2026 at 8:21 pm

    Ted wrote:

    For this, it should validate 1 or empty value. Am I correct?

    Yes, that is correct.

    Ted wrote:

    When I attempt to execute it in Python, it returns true only for 1111 but not 11 or empty value. Am I wrong somewhere?

    The empty string does get matched. The following snippet:

    #!/usr/bin/env python
    import re
    
    for n in xrange(0, 51):
      ones = '1' * n
      matches = re.match(r'^1?$|^(11+?)\1+$', ones)
      if matches:
        div1 = n if matches.group(1) is None else len(matches.group(1))
        div2 = 0 if div1 is 0 else len(ones)/div1
        print "[{0:2}]:{1:2} * {2:2} = '{3}'".format(n, div1, div2, ones)
    

    will print:

    [ 0]: 0 *  0 = ''
    [ 1]: 1 *  1 = '1'
    [ 4]: 2 *  2 = '1111'
    [ 6]: 2 *  3 = '111111'
    [ 8]: 2 *  4 = '11111111'
    [ 9]: 3 *  3 = '111111111'
    [10]: 2 *  5 = '1111111111'
    [12]: 2 *  6 = '111111111111'
    [14]: 2 *  7 = '11111111111111'
    [15]: 3 *  5 = '111111111111111'
    [16]: 2 *  8 = '1111111111111111'
    [18]: 2 *  9 = '111111111111111111'
    [20]: 2 * 10 = '11111111111111111111'
    [21]: 3 *  7 = '111111111111111111111'
    [22]: 2 * 11 = '1111111111111111111111'
    [24]: 2 * 12 = '111111111111111111111111'
    [25]: 5 *  5 = '1111111111111111111111111'
    [26]: 2 * 13 = '11111111111111111111111111'
    [27]: 3 *  9 = '111111111111111111111111111'
    [28]: 2 * 14 = '1111111111111111111111111111'
    [30]: 2 * 15 = '111111111111111111111111111111'
    [32]: 2 * 16 = '11111111111111111111111111111111'
    [33]: 3 * 11 = '111111111111111111111111111111111'
    [34]: 2 * 17 = '1111111111111111111111111111111111'
    [35]: 5 *  7 = '11111111111111111111111111111111111'
    [36]: 2 * 18 = '111111111111111111111111111111111111'
    [38]: 2 * 19 = '11111111111111111111111111111111111111'
    [39]: 3 * 13 = '111111111111111111111111111111111111111'
    [40]: 2 * 20 = '1111111111111111111111111111111111111111'
    [42]: 2 * 21 = '111111111111111111111111111111111111111111'
    [44]: 2 * 22 = '11111111111111111111111111111111111111111111'
    [45]: 3 * 15 = '111111111111111111111111111111111111111111111'
    [46]: 2 * 23 = '1111111111111111111111111111111111111111111111'
    [48]: 2 * 24 = '111111111111111111111111111111111111111111111111'
    [49]: 7 *  7 = '1111111111111111111111111111111111111111111111111'
    [50]: 2 * 25 = '11111111111111111111111111111111111111111111111111'
    

    And the input 11 is not matched because 11 is matched in group 1 ((11+?)), which should then be repeated at least once (\1+), which is not the case (it is not repeated).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this regular expression below to validate a security question where some one
I have a this regular expression below for some input name fields. How do
I have below code to insert a style into DOM (there is a use
What do I have to change in this regular expression so that in both
I have just coded the below regular expression. I have a mini rich text
I have to write a regular expression that will match the following patterns, it
I want VB.NET regular expression for below format 7966-591739 I mean user must have
I am trying to learn more about regular expressions I have one below that
I have below query I am trying to show message 'No SubSource for this
I want to have the regular expression that makes sure the beginning of the

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.