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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:40:42+00:00 2026-06-08T13:40:42+00:00

I am trying to allow the user to do this: Lets say initially the

  • 0

I am trying to allow the user to do this:

Lets say initially the text says:

"hello world hello earth"

when the user searches for “hello” it should display:

|hello| world |hello| earth

here’s what I have:

m = re.compile(pattern)
i =0
match = False
while i < len(self.fcontent):
    content = " ".join(self.fcontent[i])
    i = i + 1;
    for find in m.finditer(content):    
        print i,"\t"+content[:find.start()]+"|"+content[find.start():find.end()]+"|"+content[find.end():]
        match = True
        pr = raw_input( "(n)ext, (p)revious, (q)uit or (r)estart? ")
        if (pr == 'q'):
            break
        elif (pr == 'p'):
            i = i -  2
        elif (pr == 'r'):
            i = 0
if match is False:
    print "No matches in the file!"

where :

pattern = user specified pattern
fcontent = contents of a file read in and stored as array of words and lines e.g:
[['line','1'],['line','2','here'],['line','3']]

however it prints

|hello| world hello earth
hello world |hello| earth

how can i merge the two lines to be displayed as one?
Thanks

Edit:

This a part of a larger search function where the pattern..in this case the word “hello” is passed from the user, so I have to use regex search/match/finditer to find the pattern. The replace and other methods sadly won’t work because the user can choose to search for “[0-9]$” and that would mean to put the ending number between |’s

  • 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-06-08T13:40:43+00:00Added an answer on June 8, 2026 at 1:40 pm

    Ok, going back to original solution since OP confirmed that word would stand on its own (ie not be a substring of another word).

    target = 'hello'
    line = 'hello world hello earth'
    rep_target = '|{}|'.format(target)
    
    line = line.replace(target, rep_target)
    

    yields:

    |hello| world |hello| earth
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a ListView with CheckBox's...Th ListView should allow the user
All, I'm trying to allow a user to search for a song. Say the
In this code I'm trying to allow the user to grab the black square
So I'm trying to add some ability to my project to allow user-defined properties
I trying to pomp showPermissionDialog for allow the user to post something in a
I am trying to set up a page that will allow the user to
I'm trying to modifty the Flex Tree control to allow a user to select
i'm trying to add some navigation arrows which allow the user to go to
I am trying to expand the devise registration form to allow a new user
I'm new to XNA and am trying to allow the user to edit an

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.