By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

regex "%s" and how to escape the %

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
(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

P: n/a
>> 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.