regex "%s" and how to escape the %

J

Jeff Jarrell

I want to use the regex.replace for a string containing "%s"

I can't seem to get the "%s" escaped. I tried a normal "\%s" but that
doesn't seem to do it. Picks up any "s".
---------------------------------------------------------------------
Dim options As RegexOptions = RegexOptions.None
Dim oRegex As Regex = New Regex("[\%s]", options)

dim s as string

s = oRegex.Replace("hellos to you and %s", "others", 1) '---- note the 1
do replace only one time

I'd like s to be "hellos to you and others" but I get

"helloothers and %s"

Ideas?
 
S

stand__sure

(not that this matters -- it isn't the source of the error -- but) why
are you escaping the %?

the problem is the character class [ ] construct -- your regex is
matching either the character "%" or the character "s"

using the text of your original post and this expression

Public Dim oRegex as Regex = New Regex( _
"%s", _
RegexOptions.None _
)

the result is as follows:

I want to use the regex.replace for a string containing "others"

I can't seem to get the "others" escaped. I tried a normal "\others"
but that
doesn't seem to do it. Picks up any "s".
---------------------------------------------------------------------
Dim options As RegexOptions = RegexOptions.None
Dim oRegex As Regex = New Regex("[\others]", options)

dim s as string

s = oRegex.Replace("hellos to you and others", "others", 1) '----
note the 1
do replace only one time

I'd like s to be "hellos to you and others" but I get

"helloothers and others"

Ideas?
 
J

Jeff Jarrell

the problem is the character class [ ] construct
yep thats it.
It wasn't working, so i thought that "%" was a special regex character, like
a "." or a "?".

thanks for the quick read.

stand__sure said:
(not that this matters -- it isn't the source of the error -- but) why
are you escaping the %?

the problem is the character class [ ] construct -- your regex is
matching either the character "%" or the character "s"

using the text of your original post and this expression

Public Dim oRegex as Regex = New Regex( _
"%s", _
RegexOptions.None _
)

the result is as follows:

I want to use the regex.replace for a string containing "others"

I can't seem to get the "others" escaped. I tried a normal "\others"
but that
doesn't seem to do it. Picks up any "s".
---------------------------------------------------------------------
Dim options As RegexOptions = RegexOptions.None
Dim oRegex As Regex = New Regex("[\others]", options)

dim s as string

s = oRegex.Replace("hellos to you and others", "others", 1) '----
note the 1
do replace only one time

I'd like s to be "hellos to you and others" but I get

"helloothers and others"

Ideas?
 

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

Similar Threads

RegEx and Vb.net /// "Unrecognized escape sequence" 6
Regex in C# 4
Help with regular expression 2
Non matching regex? 1
Regex woes 8
regex pattern 4
Regex references 4
Regular Expression question 5

Top