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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:14:25+00:00 2026-06-08T12:14:25+00:00

I should preface this by stating that I’m working with VB6 & RegExp I’m

  • 0

I should preface this by stating that I’m working with VB6 & RegExp

I’m attempting to find and substitute whole words, by “whole words” I mean a valid match is not a substring of another word, although some special characters will be ok. I’m a novice at regular expressions. This is what I was trying:

([^a-z]+)(Foo)([^a-z]+)

It seems close but I’m having some trouble in certain situations.

For example, if I find the string

Foo Foo

or

Foo(Foo)

or anywhere a line ends with Foo and the following line begins with Foo

This is a line with Foo
Foo starts the next line

In any of these cases only the first Foo is matched.

Well, maybe it isn’t a problem with the match but rather my replace method. I don’t know exactly how I can verify that. I’m using groups to replace whatever bounding char is matched by the expression, like so:

regEX.Replace(source, "$1" & newstring & "$3")

So in summary I want to avoid matching:
FooBar
BarFoo

Any of the following would be valid matches:

Foo Foo
Foo Bar
Foo_bar
Foo.bar
Foo, bar
Foo(bar)
Foo(Foo)

If anyone can kindly show me the proper way to do this I would much appreciate it!

edited

Looks like I spoke a little too soon regarding the first solution below. After a little testing and some further reading, I see that underscore is a word char and thus the above pattern won’t match it. I came up with this which does the trick, is there a better way?

(\b)(Foo)(\b|_)

regEX.Replace(source, "$1" & newstring & "$3")

It works, but seems a little sloppy.

  • 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-08T12:14:26+00:00Added an answer on June 8, 2026 at 12:14 pm

    Use the “word boundary” expression \b.

    Perhaps something as simple as this will do:

    (.*)\bFoo\b(.*)
    

    FYI, the word boundary expression \b is a zero-width match between a word character \w and a non-word character [^\w] or visa versa, and consumes no input.


    Underscore and digit characters are considered “word characters”, so Foo_Bar, Bar_Foo, and Foo123 wouldn’t match. To rectify that, so that any non-letter is considered “end of word” (including start and end of input), use look arounds:

    (?i)(.*(?<![^a-z]))Foo((?![^a-z]).*)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I should preface this question by saying that I am new to JavaServer Pages.
I should preface this by saying I'm working on a pocket PC app and
I should preface this by saying that I am an undergrad CS student with
I should preface this with the fact that I know virtually nothing about javascript
Preface : I'm honestly not sure if this should be on StackOverflow, SuperUser or
I will preface this question with the disclaimer that I know there are many
BACKGROUND: I should preface this by saying I'm not trying to get someone to
First off, let me preface this question by saying that my professor is firmly
I should preface this question by saying I think the answer is probably no,
Let me preface this question with I know this is not something that I

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.