Regular Expression Woes

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?
 
C

Chris R. Timmons

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top