Regular expression problem

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

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 said:
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
 
Great, thanks! This did the trick.

Hans Kesting said:
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
 
true!

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

/Lars
 
Back
Top