Regular expression problem

Discussion in 'ASP .Net' started by Dan, Apr 15, 2004.

  1. Dan

    Dan Guest

    I'm trying to remove the word 'AND' (apostrophes included) from a string but
    can't seem to get the correct expression.

    I've got:

    Dim regRemove As New System.Text.RegularExpressions.Regex("\b'AND'\b")
    Dim strInput As String = "One 'AND' Two"
    Response.Write(regRemove.Replace(strInput, ""))

    But it doesn't work. Do the apostrophes in my regex need to be escaped?
    I've tried \' and '' but to no avail.

    Any ideas?

    Thanks
     
    Dan, Apr 15, 2004
    #1
    1. Advertising

  2. Dan

    Hans Kesting Guest

    "Dan" <> wrote in message news:%...
    > I'm trying to remove the word 'AND' (apostrophes included) from a string but
    > can't seem to get the correct expression.
    >
    > I've got:
    >
    > Dim regRemove As New System.Text.RegularExpressions.Regex("\b'AND'\b")
    > Dim strInput As String = "One 'AND' Two"
    > Response.Write(regRemove.Replace(strInput, ""))
    >
    > But it doesn't work. Do the apostrophes in my regex need to be escaped?
    > I've tried \' and '' but to no avail.
    >
    > Any ideas?
    >
    > Thanks
    >
    >


    The quote is OK, the problem is with the \b: it matches a boundary
    between alphanumeric and non-alphanumeric. Space and ' are both
    non-alphenumeric, so there is no boundary!

    You could try \B: match on non-boundary.

    Hans Kesting
     
    Hans Kesting, Apr 15, 2004
    #2
    1. Advertising

  3. Dan

    Lars Netzel Guest

    Why not just do this?

    NewString = Replace(inputstring, "AND", "")

    /Lars



    "Dan" <> skrev i meddelandet
    news:%...
    > I'm trying to remove the word 'AND' (apostrophes included) from a string

    but
    > can't seem to get the correct expression.
    >
    > I've got:
    >
    > Dim regRemove As New System.Text.RegularExpressions.Regex("\b'AND'\b")
    > Dim strInput As String = "One 'AND' Two"
    > Response.Write(regRemove.Replace(strInput, ""))
    >
    > But it doesn't work. Do the apostrophes in my regex need to be escaped?
    > I've tried \' and '' but to no avail.
    >
    > Any ideas?
    >
    > Thanks
    >
    >
     
    Lars Netzel, Apr 15, 2004
    #3
  4. Dan

    Dan Guest

    Thanks, but that won't cope with something like:

    SUN AND SAND

    "Lars Netzel" <> wrote in message
    news:O7x$...
    > Why not just do this?
    >
    > NewString = Replace(inputstring, "AND", "")
    >
    > /Lars
    >
    >
    >
    > "Dan" <> skrev i meddelandet
    > news:%...
    > > I'm trying to remove the word 'AND' (apostrophes included) from a string

    > but
    > > can't seem to get the correct expression.
    > >
    > > I've got:
    > >
    > > Dim regRemove As New System.Text.RegularExpressions.Regex("\b'AND'\b")
    > > Dim strInput As String = "One 'AND' Two"
    > > Response.Write(regRemove.Replace(strInput, ""))
    > >
    > > But it doesn't work. Do the apostrophes in my regex need to be escaped?
    > > I've tried \' and '' but to no avail.
    > >
    > > Any ideas?
    > >
    > > Thanks
    > >
    > >

    >
    >
     
    Dan, Apr 15, 2004
    #4
  5. Dan

    Dan Guest

    Great, thanks! This did the trick.

    "Hans Kesting" <> wrote in message
    news:...
    >
    > "Dan" <> wrote in message

    news:%...
    > > I'm trying to remove the word 'AND' (apostrophes included) from a string

    but
    > > can't seem to get the correct expression.
    > >
    > > I've got:
    > >
    > > Dim regRemove As New System.Text.RegularExpressions.Regex("\b'AND'\b")
    > > Dim strInput As String = "One 'AND' Two"
    > > Response.Write(regRemove.Replace(strInput, ""))
    > >
    > > But it doesn't work. Do the apostrophes in my regex need to be escaped?
    > > I've tried \' and '' but to no avail.
    > >
    > > Any ideas?
    > >
    > > Thanks
    > >
    > >

    >
    > The quote is OK, the problem is with the \b: it matches a boundary
    > between alphanumeric and non-alphanumeric. Space and ' are both
    > non-alphenumeric, so there is no boundary!
    >
    > You could try \B: match on non-boundary.
    >
    > Hans Kesting
    >
    >
    >
     
    Dan, Apr 15, 2004
    #5
  6. Dan

    Lars Netzel Guest

    true!

    Can you use a comma instead of AND to separate the words in the string and
    just split everything into an array?

    /Lars

    "Dan" <> skrev i meddelandet
    news:%...
    > Thanks, but that won't cope with something like:
    >
    > SUN AND SAND
    >
    > "Lars Netzel" <> wrote in message
    > news:O7x$...
    > > Why not just do this?
    > >
    > > NewString = Replace(inputstring, "AND", "")
    > >
    > > /Lars
    > >
    > >
    > >
    > > "Dan" <> skrev i meddelandet
    > > news:%...
    > > > I'm trying to remove the word 'AND' (apostrophes included) from a

    string
    > > but
    > > > can't seem to get the correct expression.
    > > >
    > > > I've got:
    > > >
    > > > Dim regRemove As New System.Text.RegularExpressions.Regex("\b'AND'\b")
    > > > Dim strInput As String = "One 'AND' Two"
    > > > Response.Write(regRemove.Replace(strInput, ""))
    > > >
    > > > But it doesn't work. Do the apostrophes in my regex need to be

    escaped?
    > > > I've tried \' and '' but to no avail.
    > > >
    > > > Any ideas?
    > > >
    > > > Thanks
    > > >
    > > >

    > >
    > >

    >
    >
     
    Lars Netzel, Apr 15, 2004
    #6
    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. godfather2

    Regular Expression problem

    godfather2, Jul 20, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,001
    Shawn Corey
    Jul 20, 2003
  2. VSK
    Replies:
    2
    Views:
    2,299
  3. Nazir
    Replies:
    3
    Views:
    5,070
    jayendra gadhavi
    Jan 2, 2008
  4. =?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:
    850
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,968
    vforvikash
    Dec 29, 2008
Loading...

Share This Page