Regular Expression Woes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephajn Craig

    #1

    Regular Expression Woes

    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 Craig


  • Chris R. Timmons

    #2
    Re: Regular Expression Woes

    "Stephajn Craig" <s.craig@NOSPAM funsunvacations .com> wrote in
    news:OJk#yeITDH A.2460@TK2MSFTN GP10.phx.gbl:
    [color=blue]
    > 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?
    >[/color]

    Stephajn,

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

    \bEngland\b

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

    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.

    Comment

    Working...