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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:48:21+00:00 2026-05-24T04:48:21+00:00

I am getting trouble with the following matter.Let’s say, I have some string in

  • 0

I am getting trouble with the following matter.Let’s say, I have some string in two list in a dictionary:

 left                                right
british                             7
cuneate nucleus                     Medulla oblongata
Motoneurons                         anterior

And I have some test lines in a file as like below:

<s id="69-7">British Meanwhile is the studio 7 album by british pop band 10cc 7.</s>
<s id="5239778-2">Medulla oblongata,the name refers collectively to the cuneate nucleus and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.</s>
<s id="21120-99">Terior horn cells, motoneurons located in the spinal.</s>

I want to get output as like following way:

<s id="69-7"><w2>British</w2> Meanwhile is the studio <w2>7</w2> album by <w1>british</w1> pop band 10cc <w2>7</w2>.</s>
<s id="5239778-2"><w2>Medulla oblongata</w2>,the name refers collectively to the <w1>cuneate nucleus</w1> and gracile nucleus, which are present at the junction between the spinal cord and the <w2>medulla oblongata</w2>.</s>

I tried with the following code:

import re

def textReturn(left, right):
    text = ""
    filetext = open(text.xml, "r").read()
    linelist = re.split(u'[\n|\r\n]+',filetext)

    for i in linelist:
        left = left.strip()
        right = right.strip()

        if left in i and right in i:
            i1 = re.sub('(?i)(\s+)(%s)(\s+)'%left, '\\1<w1>\\2</w1>\\3', i)
            i2 = re.sub('(?i)(\s+)(%s)(\s+)'%right, '\\1<w2>\\2</w2>\\3', i1)
            text = text + i2 + "\n"         
    return text   

But it gives me:

'<s id="69-7">British meanwhile is the studio <w2>7</w2> album by <w1>British</w1> pop band 10cc 7.</s>'.
<s id="5239778-2">Medulla oblongata,the name refers collectively to the <w1>cuneate nucleus</w1> and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.</s>
<s id="21120-99">Terior horn cells, <w1>motoneurons</w2> located in the spinal.</s>

i.e It can’t tag if there are string at the beginning & end .

Also,I just want to get return those line ,which matches both left & right strings, NOT others line.

Any solution please! Thanks a lot!!!

  • 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-24T04:48:22+00:00Added an answer on May 24, 2026 at 4:48 am

    It doesn’t tag at the beginning and the end because you expect one or more spaces before and after your keywords.

    Instead of \s+, use \b (word break).

    ADDENDUM

    Actual code:

    import re
    
    dict = [('british','7'),('cuneate nucleus','Medulla oblongata'),('Motoneurons','anterior')]
    
    filetext = """<s id="69-7">British Meanwhile is the studio 7 album by british pop band 10cc 7.</s>
    <s id="5239778-2">Medulla oblongata,the name refers collectively to the cuneate nucleus and gracile nucleus, which are present at the junction between the spinal cord and the medulla oblongata.</s>
    <s id="21120-99">Terior horn cells, motoneurons located in the spinal.</s>
    """
    
    linelist = re.split(u'[\n|\r\n]+', filetext)
    
    s_tag = re.compile(r"(<s[^>]+>)(.*?)(</s>)")
    
    for i in range(3):
        left, right = dict[i]
    
        line_parts = re.search(s_tag, linelist[i])
        start = line_parts.group(1)
        content = line_parts.group(2)
        end = line_parts.group(3)
    
        left_match = "(?i)\\b(%s)\\b" % left
        right_match = "(?i)\\b(%s)\\b" % right
        if re.search(left_match, content) and re.search(right_match, content):
            line1 = re.sub(left_match, '<w1>\\1</w1>', content)
            line2 = re.sub(right_match, '<w2>\\1</w2>', line1)
            print(line_parts.group(1) + line2 + line_parts.group(3))
    

    This is the basis for a short-term solution, but long-term you should try out the XML parser approach.

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

Sidebar

Related Questions

I have been having some trouble getting the following to work: <IfModule mod_rewrite.c> DirectoryIndex
I'm having some trouble getting a view to flip. I have the following code
I'm getting some trouble binding a date from QueryString : I have the following
I am having trouble getting the bulk uploader to work. I have been following
I am getting trouble with directory listing.Suppose, I have a directory with some subdirectory(named
I have some trouble getting a good folder structure in my project and i
I'm quite new to MDX and im having some trouble getting the following t-sql
I have a trouble getting the following result from a xml/xslt transformation: <root> <node>
I'm having trouble getting Apache/WSGI to use my VirtualEnv. I have added the following
I'm having trouble getting a value from an xml string. $query_return_string contains the following

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.