RegExp finds and replaces whole words only

I must state this by stating that I am working with VB6 and RegExp

I am trying to find and replace whole words with "whole words". I mean, a valid match is not a substring of another word, although some special characters will be fine. I am new to regular expressions. This is what I tried:

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

It seems close, but in some situations I have problems.

For example, if I find a string

Foo Foo

or

Foo(Foo)

or somewhere the line ends with foo and the next line starts 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 this is not a problem with the match, but my replacement method. I don’t know exactly how to verify this. I use groups to replace any bounding char matching an expression, for example:

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

, : FooBar BarFoo

:

Foo Foo
Foo Bar
foo_bar
Foo.bar
Foo, bar
Foo ()
Foo (Foo)

- , !

, . , , - char, , , . , , ?

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

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

, .

+5
1

" " \b.

, - , :

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

FYI, \b - \w [^\w] , .


" ", Foo_Bar, Bar_Foo Foo123 . , " " ( ), :

(?i)(.*(?<![^a-z]))Foo((?![^a-z]).*)
+6

All Articles