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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:15:09+00:00 2026-06-10T05:15:09+00:00

What is the best way to produce a highlighted string found within another string?

  • 0

What is the best way to produce a highlighted string found within another string?

I want to ignore all character that are not alphanumeric but retain them in the final output.

So for example a search for ‘PC3000’ in the following 3 strings would give the following results:

   ZxPc 3000L = Zx<font color='red'>Pc 3000</font>L

   ZXP-C300-0Y  =  ZX<font color='red'>P-C300-0</font>Y

   Pc3 000  =   <font color='red'>Pc3 000</font>

I have the following code but the only way i can highlight the search within the result is to remove all the whitespace and non alphanumeric characters and then set both strings to lowercase. I’m stuck!

public string Highlight(string Search_Str, string InputTxt)
    {

        // Setup the regular expression and add the Or operator.
        Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);

        // Highlight keywords by calling the delegate each time a keyword is found.
        string Lightup = RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));

        if (Lightup == InputTxt)
        {
            Regex RegExp2 = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
            RegExp2.Replace(" ", "");

            Lightup = RegExp2.Replace(InputTxt.Replace(" ", ""), new MatchEvaluator(ReplaceKeyWords));

            int Found = Lightup.IndexOf("<font color='red'>");

            if (Found == -1)
            {
                Lightup = InputTxt;
            }

        }

        RegExp = null;
        return Lightup;
    }

    public string ReplaceKeyWords(Match m)
    {
        return "<font color='red'>" + m.Value + "</font>";
    }

Thanks guys!

  • 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-10T05:15:11+00:00Added an answer on June 10, 2026 at 5:15 am

    Alter your search string by inserting an optional non-alphanumeric character class ([^a-z0-9]?) between each character. Instead of PC3000 use

    P[^a-z0-9]?C[^a-z0-9]?3[^a-z0-9]?0[^a-z0-9]?0[^a-z0-9]?0
    

    This matches Pc 3000, P-C300-0 and Pc3 000.

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

Sidebar

Related Questions

If I have a JavaScript string (potentially containing Unicode characters), what's the best way
What is the best way to produce colour correct raster files with ghostscript? The
This question is related to this one: Best way to store product colors in
What is the best way to maintain upgrade scripts between versions of a product?
What is the best way to utilize OpenMP with a matrix-vector product? Would the
Best way to match 2 pages for Google analytics. Pages to Query: 1.) this_google.html
The best way I think to explain this is to tell you what my
The best way to explain my problem is by a example. I have a
Any best way to run the jasmine HTML reporter with browserify styled code? I
the best way to explain is with example so: this is the model public

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.