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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:23:16+00:00 2026-06-13T02:23:16+00:00

BACKGROUND I have a scenario where I must repeatedly find certain words in text,

  • 0

BACKGROUND

I have a scenario where I must repeatedly find certain words in text, over and over.
I have currently used a series of regular Expressions in a format like this…

"((^)|(\W))(?<c>Word1)((\W)|($))"

"((^)|(\W))(?<c>NextWord)((\W)|($))"

"((^)|(\W))(?<c>AnotherWord)((\W)|($))"

…

This list of Regex objects is them looped through with a chunk of data and the matches are pulled out (one loop for one regex.matches(data) call)

I have done everything I can to optimize them, such as Compiling them before hand.

However the list is growing longer and I decided to start making larger compiled regular expressions to optimize the process. such as…

"((^)|(\W))(?<c>((Word1)|(NextWord)|(AnotherWord)))((\W)|($))"

This provides a HUGE speed imporvement, however there is a side effect I cannot figure out how to correct.

When the words are in the data side by side (such as space delimited. eg. “Word1 NextWord AnotherWord”) the second word is missed in the capture because the regex for “Word1” also includes the trailing space. The match that could occur for “NextWord” no longer has the Leading space because it’s part of the previous match.

QUESTION

Can anyone alter this Regular expression (.net format)

Pattern = "((^)|(\W))(?<c>((Word1)|(NextWord)|(AnotherWord)))((\W)|($))"

to work to capture all the words in this list below with a single call to “.matches(data)”
Where

data = "Word1 NextWord AnotherWord" 

? (without sacrificing the efficiency gain)

RESULTS

Just thought I would mention this. After applying the suggested answer/correction with the look ahead and look behind, which I now know how to use 🙂 , the code I just modified has improved in speed by 347x (0.00347% of old testing speed). Which is definitly something to remember when you get into multiple expressions. Very happy.

  • 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-13T02:23:17+00:00Added an answer on June 13, 2026 at 2:23 am

    You may want to use either boundary checks or lookahead/lookbehind, so that the match doesn’t consume the whitespace but checks it.

    Like so:

    Pattern = @"\b(Word1|NextWord|AnotherWord)\b"
    

    Or with the lookbehind and lookahead:

    Pattern = @"(?<=\W|^)(Word1|NextWord|AnotherWord)(?=\W|$)"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My scenario: I have one color background image JPG. I have one black text
I have the following scenario: a Bitmap that is used as background and another
Scenario I have a background worker in my application that runs off and does
Skip to the specific question as needed. Some background: The scenario: I have a
This is the scenario: I have one image background set on an activity. On
I have a scenario where I need a transparent background image but I have
I have this scenario: html: <a>text</a> <a>text</a> <a>text</a> <a>text</a> <h4>hi there</h4> <h4>hi there</h4> <h4>hi
I have a simple scenario where a NSTimer is scheduled on a background thread
My scenario: I have a background thread that polls for changes and periodically updates
I have this rspec scenario: background do Factory.create(:project, :name => NAME, :url_name => url

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.