473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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
2 1291
(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Flos | last post by:
Hello! I want to make a validation for a string which can be only letters AND space. for example "apple house", but not "apple3 house" i have found if (preg_match ("//", $string)) return...
16
by: Andrew Baker | last post by:
I am trying to write a function which provides my users with a file filter. The filter used to work just using the VB "Like" comparision, but I can't find the equivilant in C#. I looked at...
5
by: Mark Johnson | last post by:
Regex("@("); brings an error (missing ")"). How do you serarch for a ( with Regex ? Mark Johnson, Berlin Germany mj10777@mj10777.de
16
by: Mark Chambers | last post by:
Hi there, I'm seeking opinions on the use of regular expression searching. Is there general consensus on whether it's now a best practice to rely on this rather than rolling your own (string)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.