Regular Expression Woes

Discussion in 'ASP .Net' started by Stephajn Craig, Jul 17, 2003.

  1. 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
     
    Stephajn Craig, Jul 17, 2003
    #1
    1. Advertising

  2. "Stephajn Craig" <> wrote in
    news:OJk#:

    > 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.
    -------------
    C.R. Timmons Consulting, Inc.
    http://www.crtimmonsinc.com/
     
    Chris R. Timmons, Jul 18, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,310
  2. =?Utf-8?B?SmltIE1hY2U=?=

    Regular Expression Woes

    =?Utf-8?B?SmltIE1hY2U=?=, May 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    320
    =?Utf-8?B?SmltIE1hY2U=?=
    May 25, 2004
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    851
    Alan Moore
    Dec 2, 2005
  4. Mark (News)

    Regular expression woes

    Mark (News), Feb 4, 2005, in forum: Perl Misc
    Replies:
    23
    Views:
    224
    Ilya Zakharevich
    Feb 8, 2005
  5. Mark (News)

    Regular expression woes

    Mark (News), Feb 4, 2005, in forum: Javascript
    Replies:
    23
    Views:
    221
    Ilya Zakharevich
    Feb 8, 2005
Loading...

Share This Page