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

The Archive Base Latest Questions

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

I need a regex that gives me the word before and after a specific

  • 0

I need a regex that gives me the word before and after a specific word, included the search word itself.

Like: “This is some dummy text to find a word” should give me a string of “dummy text to” when text is my search word.

Another question, it’s possible that the string provided will contain more then once the search word so I must be able to retrieve all matches in that string with C#.

Like “This is some dummy text to find a word in a string full with text and words”
Should return:

  • “dummy text to”
  • “with text and”

EDIT:
Actually I should have all the matches returned that contain the search word.
A few examples:
Text is too read. -> Text is

Read my text. -> my text

This is a text-field example -> a text-field example

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

    EDIT:

    If you want to grab all the content from the space before first word to the space after the word use:

    (?:\S+\s)?\S*text\S*(?:\s\S+)?
    

    A simple tests:

    string input = @"
        This is some dummy text to find a word in a string full with text and words
        Text is too read
        Read my text.
        This is a text-field example
        this is some dummy la@text.be to read";
    
    var matches = Regex.Matches(
        input,
        @"(?:\S+\s)?\S*text\S*(?:\s\S+)?",
        RegexOptions.IgnoreCase
    );
    

    the matches are:

    dummy text to
    with text and
    Text is
    my text.
    a text-field example
    dummy la@text.be to
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regex that will ensure the the user input looks like this
I just need to verify that this regex statement will do what I want.
I need a regex that will give me the following results from each example
Given a file path such as: \\server\folder_A\folder_B\etc\more.mov I need a regex that'll give me
I need a Regex that will match a java method declaration. I have come
I need a regex that matches all strings ending in .cs, but if they
I need a regex that also matches Chinese, Greek, Russian, ... letters. What I
I need a regex that matches the following (the brackets indicate I want to
I need a regex that will match blahfooblah but not blahfoobarblah I want it
I need a regex that allows numbers and optional commas, but the entire length

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.