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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:29:43+00:00 2026-05-11T21:29:43+00:00

I’m suppose to capture everything inside a tag and the next lines after it,

  • 0

I’m suppose to capture everything inside a tag and the next lines after it, but it’s suppose to stop the next time it meets a bracket. What am i doing wrong?

import re #regex

regex = re.compile(r"""
         ^                    # Must start in a newline first
         \[\b(.*)\b\]         # Get what's enclosed in brackets 
         \n                   # only capture bracket if a newline is next
         (\b(?:.|\s)*(?!\[))  # should read: anyword that doesn't precede a bracket
       """, re.MULTILINE | re.VERBOSE)

haystack = """
[tab1]
this is captured
but this is suppose to be captured too!
@[this should be taken though as this is in the content]

[tab2]
help me
write a better RE
"""
m = regex.findall(haystack)
print m

what im trying to get is:
[(‘tab1’, ‘this is captured\nbut this is suppose to be captured too!\n@[this should be taken though as this is in the content]\n’, ‘[tab2]’,’help me\nwrite a better RE\n’)]

edit:

regex = re.compile(r"""
             ^           # Must start in a newline first
             \[(.*?)\]   # Get what's enclosed in brackets 
             \n          # only capture bracket if a newline is next
             ([^\[]*)    # stop reading at opening bracket
        """, re.MULTILINE | re.VERBOSE)

this seems to work but it’s also trimming the brackets inside the content.

  • 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-11T21:29:43+00:00Added an answer on May 11, 2026 at 9:29 pm

    Python regex doesn’t support recursion afaik.

    EDIT: but in your case this would work:

    regex = re.compile(r"""
             ^           # Must start in a newline first
             \[(.*?)\]   # Get what's enclosed in brackets 
             \n          # only capture bracket if a newline is next
             ([^\[]*)    # stop reading at opening bracket
        """, re.MULTILINE | re.VERBOSE)
    

    EDIT 2: yes, it doesn’t work properly.

    import re
    
    regex = re.compile(r"""
        (?:^|\n)\[             # tag's opening bracket  
            ([^\]\n]*)         # 1. text between brackets
        \]\n                   # tag's closing bracket
        (.*?)                  # 2. text between the tags
        (?=\n\[[^\]\n]*\]\n|$) # until tag or end of string but don't consume it
        """, re.DOTALL | re.VERBOSE)
    
    haystack = """[tag1]
    this is captured [not a tag[
    but this is suppose to be captured too!
    [another non-tag
    
    [tag2]
    help me
    write a better RE[[[]
    """
    
    print regex.findall(haystack)
    

    I do agree with viraptor though. Regex are cool but you can’t check your file for errors with them. A hybrid perhaps? 😛

    tag_re = re.compile(r'^\[([^\]\n]*)\]$', re.MULTILINE)
    tags = list(tag_re.finditer(haystack))
    
    result = {}
    for (mo1, mo2) in zip(tags[:-1], tags[1:]):
        result[mo1.group(1)] = haystack[mo1.end(1)+1:mo2.start(1)-1].strip()
    result[mo2.group(1)] = haystack[mo2.end(1)+1:].strip()
    
    print result
    

    EDIT 3: That’s because ^ character means negative match only inside [^squarebrackets]. Everywhere else it means string start (or line start with re.MULTILINE). There’s no good way for negative string matching in regex, only character.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

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.