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

The Archive Base Latest Questions

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

So what I’m trying to do is replace a string keyword with <b>keyword</b> in

  • 0

So what I’m trying to do is replace a string “keyword” with
"<b>keyword</b>"
in a larger string.

Example:

myString = “HI there. You should higher that person for the job. Hi hi.”

keyword = “hi”

result I would want would be:

result = "<b>HI</b> there. You should higher that person for the job.
<b>Hi</b> <b>hi</b>."

I will not know what the keyword until the user types the keyword
and won’t know the corpus (myString) until the query is run.

I found a solution that works most of the time, but has some false positives,
namely it would return "<b>hi<b/>gher"which is not what I want. Also note that I
am trying to preserve the case of the original text, and the matching should take
place irrespective of case. so if the keyword is “hi” it should replace
HI with <b>HI</b> and hi with <b>hi</b>.

The closest I have come is using a slightly derived version of this:
http://code.activestate.com/recipes/576715/
but I still could not figure out how to do a second pass of the string to fix all of the false positives mentioned above.

Or using the NLTK’s WordPunctTokenizer (which simplifies some things like punctuation)
but I’m not sure how I would put the sentences back together given it does not
have a reverse function and I want to keep the original punctuation of myString. Essential, doing a concatenation of all the tokens does not return the original
string. For example I would not want to replace “7 – 7” with “7-7” when regrouping the tokens into its original text if the original text had “7 – 7”.

Hope that was clear enough. Seems like a simple problem, but its a turned out a little more difficult then I thought.

  • 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-11T16:07:44+00:00Added an answer on May 11, 2026 at 4:07 pm

    This ok?

    >>> import re
    >>> myString = "HI there. You should higher that person for the job. Hi hi."
    >>> keyword = "hi"
    >>> search = re.compile(r'\b(%s)\b' % keyword, re.I)
    >>> search.sub('<b>\\1</b>', myString)
    '<b>HI</b> there. You should higher that person for the job. <b>Hi</b> <b>hi</b>.'
    

    The key to the whole thing is using word boundaries, groups and the re.I flag.

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

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Escape the @ with a backtick (`). Write-Host sqlplus username/password`@$args… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer No. That's why I often make a .bat or .sh… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer It depends on the grammar. You can use pyparsing instead… May 11, 2026 at 5:11 pm

Related Questions

So what I'm looking for here is something like PHP's print_r function. This is
So what I have right now is something like this: PropertyInfo[] info = obj.GetType().GetProperties(BindingFlags.Public);
So What I'm essentially trying to do is have something happen 70% of the
So what I want to do is to coordinate some effects using jQuery for
Hey so what I want to do is snag the content for the first

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.