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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:46:14+00:00 2026-05-27T02:46:14+00:00

In reference to my question Regex expression to match whole word with special characters

  • 0

In reference to my question Regex expression to match whole word with special characters not working ?,

I got an answer which said

@"(?<=^|\s)" + pattern + @"(?=\s|$)"

This works fine for all cases except 1 case. It fails when there is space in the pattern.

Assume string is “Hi this is stackoverflow” and pattern is “this ” , then it says no matches. This happens because of an empty space after the actual string in pattern.

How can we handle this ? Ideally speaking it should say one match found !

  • 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-27T02:46:14+00:00Added an answer on May 27, 2026 at 2:46 am

    Try this

    (?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))this (?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))
    

    See it here on Regexr

    This will also work for pattern that starts with a whitespace.

    Basically, what I am doing is to define a custom “word” boundary. But it is not true on a \W=>\w or a \w=>\W change, its true on a \S=>\s or a \s=>\S change!

    Here is an example in c#:

    string str = "Hi this is stackoverflow";
    string pattern = Regex.Escape("this");
    MatchCollection result = Regex.Matches(str, @"(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))" + pattern + @"(?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))", RegexOptions.IgnoreCase);
    
    Console.WriteLine("Amount of matches: " + result.Count);
    foreach (Match m in result)
    {
        Console.WriteLine("Matched: " + result[0]);
    }
    Console.ReadLine();
    

    Update:

    This “Whitespace” boundary can be done more general, so that on each side of the pattern is the same expression, like this

    (?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))
    

    In c#:

    MatchCollection result = Regex.Matches(str, @"(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))" + pattern + @"(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))", RegexOptions.IgnoreCase);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've asked this question on Wikipedia's Reference Desk , but got no answer. On
With the reference to this question:- JavaScript -Change CSS color for 5 seconds Working
I am trying to understand how Regex handles searching for literals of special characters.
my question is not about RegEx in general but about PhpStorm's search-replace-functionality with RegEx
There are some posts on this, but not an answer to this specific question.
Reference to previous question: Getting an ajax response in java from a web method
I will ask this question with reference to a specific example, I use ScaleX
This is in reference to a question I asked earlier. Aside from viewing the
This is in reference to the question previously asked The problem here is, each
(Java question) If I reference a field in an inner class, does this cause

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.