Regular Expression Woes

  • Thread starter Thread starter Stephajn Craig
  • Start date Start date
S

Stephajn Craig

I'm working for the first time with Regular Expressions. I'm trying to
return a match only when the word I'm looking for is on its own without any
characters before or after it except for punctuation marks.

So here is the pattern I've built so far.

[\s*.*.\w*](England)[\s*.\w*]

This seems to return all matches of England, except that it also returns a
match of _England. The problem is that with the \w switch, an underscore is
considered a word character. But I don't want this, because I'm going to
use the RegExp.Replace function to replace all matches with some
highlighting code. Can anyone help me complete this Search pattern?
 
I'm working for the first time with Regular Expressions. I'm
trying to return a match only when the word I'm looking for is
on its own without any characters before or after it except for
punctuation marks.

So here is the pattern I've built so far.

[\s*.*.\w*](England)[\s*.\w*]

This seems to return all matches of England, except that it also
returns a match of _England. The problem is that with the \w
switch, an underscore is considered a word character. But I
don't want this, because I'm going to use the RegExp.Replace
function to replace all matches with some highlighting code.
Can anyone help me complete this Search pattern?

Stephajn,

Use the \b (or "word boundary") escape sequence:

\bEngland\b

This will match "England", but not "_England" or "England_".

Hope this helps.

Chris.
 
Back
Top