Python search for multiple values and show with boundaries

2024/10/7 18:24:28

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 = Truepr = raw_input( "(n)ext, (p)revious, (q)uit or (r)estart? ")if (pr == 'q'):breakelif (pr == 'p'):i = i -  2elif (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

Answer

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
https://en.xdnf.cn/q/118795.html

Related Q&A

Python: create human-friendly string from a list of datetimes

Im actually looking for the opposite of this question: Converting string into datetimeI have a list of datetime objects and I want to create a human-friendly string from them, e.g., "Jan 27 and 3…

Python replace non digit character in a dataframe [duplicate]

This question already has answers here:Removing non numeric characters from a string in Python(9 answers)Closed 5 years ago.I have the following dataframe column>>> df2[Age]1 25 2 35 3 …

PyGame.error in ubuntu

I have problem with pygame and python 3 in ubuntu 12.10. When i tried load an image i got this error: pygame.error: File is not a Windows BMP fileBut in python 2.7 all works fine. I use code from http:…

Firebase Admin SDK with Flask throws error No module named firebase_admin

--- Question closedIt was my mistake, my uWSGI startup script switches to a different virtualenv.--- Original questionIm trying to publish push notifications from my Flask app server to Android APP. Se…

Recursive generator for change money - python

First, thanks for any help. I have this recursive code for counting ways of change from a list of coins and a given amount. I need to write a recursive generator code that presents the ways in every it…

Like button not recording the data [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Python, Pygame, Image manipulation: Restretch a loaded png, to be the texture for an isometric Tile

Im a 17 year old programmer, trying to program an isometric game in python, with pygame. After finishing a tile engine, working with not good looking, gimp-drawn PNGs, I wondered, if it would be possib…

TypeError: list object is not callable [duplicate]

This question already has answers here:TypeError: list object is not callable while trying to access a list(9 answers)Closed 10 years ago.I cant find the problem im facing... this is exactly what the …

Tokenise text and create more rows for each row in dataframe

I want to do this with python and pandas.Lets suppose that I have the following:file_id text 1 I am the first document. I am a nice document. 2 I am the second document. I am an even …

Is the example of the descriptor protocol in the Python 3.6 documentation incorrect?

I am new to Python and looking through its documentation I encountered the following example of the descriptor protocol that in my opinion is incorrect. .It looks like class IntField:def __get__(self, …