Regular expression problem

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
 
H

Hans Kesting

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
 
D

Dan

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
 
L

Lars Netzel

true!

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

/Lars
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top