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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:06:42+00:00 2026-05-29T10:06:42+00:00

I got some help with this earlier today but I cannot figure out the

  • 0

I got some help with this earlier today but I cannot figure out the last part of the problem I am having. This regex search returns all of the matches in the open file from the input. What I need to do is also find which part of the file that the match comes from.

Each section is opened and closed with a tag. For example one of the tags opens with <opera> and ends with </opera>. What I want to be able to do is when I find a match I want to either go backwards to the open tag or forwards to the close tag and include the contents of the tag, in this case “opera” in the output. My question is can I do this with an addition to the regular expression or is there a better way? Here is the code I have that works great already:

text = open_file.read()
#the test string for this code is "NNP^CC^NNP"
grammarList = raw_input("Enter your grammar string: ");

tags = grammarList.split("^")
tags_pattern = r"\b" + r"\s+".join(r"(\w+)/{0}".format(tag) for tag in tags) + r"\b" 
# gives you r"\b(\w+)/NNP\s+(\w+)/CC\s+(\w+)/NNP\b"

from re import findall
print(findall(tags_pattern, text))
  • 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-29T10:06:43+00:00Added an answer on May 29, 2026 at 10:06 am

    One way to do it would be to find all occurrences of your start and end section tags (say they’re <opera> and </opera>), get the indices, and compare them to each match of tags_pattern. This uses finditer which is like findall but returns indices too. Something like:

    startTags = re.finditer("<opera>",text)
    endTags   = re.finditer("</opera>",text)
    
    matches = re.finditer(tags_pattern,text)
    
    # Now, [m.start() for m in matches] gives the starting index into `text`.
    # if <opera> starts at subindices 0, 1000, 2345
    # and you get a match starting at subindex 1100,
    #  then it's in the 1000-2345 block.
    for m in matches:
        # find first
        sec = [i for i in xrange(len(startTags)) if i>startTags[i].start()]
        if len(sec)=0:
            print "err couldn't find it"
        else:
            sec = sec[0]
            print "found in\n" + text[startTags[sec].start():endTags[sec].end()]
    

    (Note: you can get the matched text with m.group() Default () has group 0 (ie entire string), and you can use m.group(i) for the ith capturing group).

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

Sidebar

Related Questions

Hi I posted this earlier and got some help but still no working solution.
I asked a question yesterday on here and got some awsome help, but I
So I've been at this mode thing for a while, and got some help
I got some problems with my patterns. Hope somebody could help me with this.
I could use some help on this problem. I'm creating an application using Symfony2
i really need some help on this. I searched the internet but couldn't find
In need some help with this little programming .. I just got 3 errors
I got some help on here last night about getting an the index of
I've got some batch files that I use to help automate the process of
With some kindly help from StackOverflow, I've got Unity Framework to create my chained

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.