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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:20:35+00:00 2026-05-26T21:20:35+00:00

I want to have a regular expression that finds the texts that are wrapped

  • 0

I want to have a regular expression that finds the texts that are “wrapped” in between “HEAD or HEADa” and “HEAD. That is, I may have a text that starts with the first word as HEAD or HEADa and the following “heads” are of type HEAD.

  1. HEAD\n\n text...text...HEAD \n\n text....text HEAD\n\n text....text .....
  2. HEADa\n\n text...text...HEAD \n\n text....text HEAD\n\n text....text .....

I want only to capture the text that are in between the “heads” therefore I have a regex with look behind and look ahead expressions looking for my “heads”. I have the following regex:

var = "HEADa", "HEAD"

my_pat = re.compile(r"(?<=^\b"+var[0]+r"|"+var[1]+r"\b) \w*\s\s(.*?)(?=\b"+var[1] +r"\b)",re.DOTALL|re.MULTILINE)

However, when I try to execute this regex, I am getting an error message saying that I cannot have variable length in the look behind expression. What is wrong with this regex?

  • 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-26T21:20:35+00:00Added an answer on May 26, 2026 at 9:20 pm

    Currently, the first part of your regex looks like this:

    (?<=^\bHEADa|HEAD\b)
    

    You have two alternatives; one matches five characters and the other matches four, and that’s why you get the error. Some regex flavors will let you do that even though they say they don’t allow variable-length lookbehinds, but not Python. You could break it up into two lookbehinds, like this:

    (?:(?<=^HEADa\b)|(?<=\bHEAD\b))
    

    …but you probably don’t need lookbehinds for this anyway. Try this instead:

    (?:^HEADa|\bHEAD)\b
    

    Whatever gets matched by the (.*?) later on will still be available through group #1. If you really need the whole of the text between the delimiters, you can capture that in group #1, and that other group will become #2 (or you can use named groups, and not have to keep track of the numbers).

    Generally speaking, lookbehind should never be your first resort. It may seem like the obvious tool for the job, but you’re usually better off doing a straight match and extracting the part you want with a capturing group. And that’s true of all flavors, not just Python; just because you can do more with lookbehinds in other flavors doesn’t mean you should.

    BTW, you may have noticed that I redistributed your word boundaries; I think this is what you really intended.

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

Sidebar

Related Questions

I have that text : <a href=/extend/themes/bizway>BizWay</a> And i want to use regular expression
I want to use regular expression to find strings in a file that have
i want a regular expression to validate string to have only text,operators and these
in my C# program, I have a regular expression textparser, that finds all occurrences
I want to get the first string that matches my regular expression. For example
I have some unicode text that I want to clean up using regular expressions.
I have a regular expression that has several matches in a textfile. I want
I have a question about the following regular expression: I want to match the
I have a textbox and a regular expression validator applied to it. I want
I have a phone number I want to match against a regular expression. 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.