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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:05:05+00:00 2026-05-18T04:05:05+00:00

I’m trying to add tags to some given query strings, and the tags should

  • 0

I’m trying to add tags to some given query strings, and the tags should wrap around all the matching strings.
For example, I want to wrap tags around all the words that match the query iphone games mac in the sentence I love downloading iPhone games from my mac. It should be I love downloading <em>iPhone games</em> from my <em>mac</em>.

Currently, I tried

sentence = "I love downloading iPhone games from my mac."
query = r'((iphone|games|mac)\s*)+'
regex = re.compile(query, re.I)
sentence = regex.sub(r'<em>\1</em> ', sentence)

The sentence outputs

I love downloading <em>games </em> on my <em>mac</em> !

Where \1 is only replace by one word (games instead of iPhone games) and there are some unnecessary spaces after the word. How do I write the regular expression to get the desired output? Thanks!

Edit:
I just realized that both Fred and Chris’s solutions have problems when I have words within words. For instance, if my query is game, then it will turn out to be <em>game</em>s while I want it not be highlighted. Another example is the in either shouldn’t be highlighted.

Edit 2:
I took Chris’ new solution and it works.

  • 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-18T04:05:06+00:00Added an answer on May 18, 2026 at 4:05 am

    First of all, to get the spaces as you want them, replace \s* with \s*? to make it non-greedy.

    First fix:

    >>> re.compile(r'(((iphone|games|mac)\s*?)+)', re.I).sub(r'<em>\1</em>', sentence)
    'I love downloading <em>iPhone</em> <em>games</em> from my <em>mac</em>.'
    

    Unfortunately, once the \s* is non-greedy, it splits phrases, as you can see. Without it, it goes like this, grouping the two together:

    >>> re.compile(r'(((iPhone|games|mac)\s*)+)').sub(r'<em>\1</em>', sentence)
    'I love downloading <em>iPhone games </em>from my <em>mac</em>.'
    

    I can’t think yet how to fix this.

    Note also that in these I have stuck in an extra set of brackets around the + so that all matches get caught – that’s the difference.

    Further update: actually, I can think of a way to get around it. You decide whether you want it like that.

    >>> regex = re.compile(r'((iphone|games|mac)(\s*(iphone|games|mac))*)', re.I)
    >>> regex.sub(r'<em>\1</em>', sentence)
    'I love downloading <em>iPhone games</em> from my <em>mac</em>.'
    

    Update: taking your point about word boundaries into account, we only need to add in a few instances of \b, the word boundary matcher.

    >>> regex = re.compile(r'(\b(iphone|games|mac)\b(\s*(iphone|games|mac)\b)*)', re.I)
    >>> regex.sub(r'<em>\1</em>', 'I love downloading iPhone games from my mac')
    'I love downloading <em>iPhone games</em> from my <em>mac</em>'
    >>> regex.sub(r'<em>\1</em>', 'I love downloading iPhone gameses from my mac')
    'I love downloading <em>iPhone</em> gameses from my <em>mac</em>'
    >>> regex.sub(r'<em>\1</em>', 'I love downloading iPhoney games from my mac')
    'I love downloading iPhoney <em>games</em> from my <em>mac</em>'
    >>> regex.sub(r'<em>\1</em>', 'I love downloading iPhoney gameses from my mac')
    'I love downloading iPhoney gameses from my <em>mac</em>'
    >>> regex.sub(r'<em>\1</em>', 'I love downloading miPhone gameses from my mac')
    'I love downloading miPhone gameses from my <em>mac</em>'
    >>> regex.sub(r'<em>\1</em>', 'I love downloading miPhone games from my mac')
    'I love downloading miPhone <em>games</em> from my <em>mac</em>'
    >>> regex.sub(r'<em>\1</em>', 'I love downloading iPhone igames from my mac')
    'I love downloading <em>iPhone</em> igames from my <em>mac</em>'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.