regex "%s" and how to escape the %

  • Thread starter Thread starter Jeff Jarrell
  • Start date Start date
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?
 
(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?
 
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?
 
Back
Top