467,092 Members | 1,229 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,092 developers. It's quick & easy.

regex "%s" and how to escape the %

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?


Nov 21 '05 #1
  • viewed: 1098
Share:
2 Replies
(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?

Nov 21 '05 #2
>> the problem is the character class [ ] construct
yep thats it.
why are you escaping, the "%s"
It wasn't working, so i thought that "%" was a special regex character, like
a "." or a "?".

thanks for the quick read.

"stand__sure" <st*********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com... (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?

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.