Search:
Namespaces
Discussions
.NET v1.1
Feedback
Problem with Replace
Messages
Related Types
This message was discovered on
ASPFriends.com 'aspngregexp' list
.
Rubric
=============================================
I've written a Replace routine using the code below. The idea is to
replace the contents of the sourceText variable with a modified version
of the searchText variable.
The problem the code goes past the Replace command, but does not fire at
all. The returned newText variable is an unmodified version of
sourceText.
Is there something I'm missing?
=============================================
//create a regular expression with the match string
Regex
regexp = new
Regex
("\b" + searchText + "\b",
RegexOptions
.IgnoreCase);
//replace the source text with this string
string newText = regexp.Replace(sourceText, "<span class='highlight'>" +
searchText + "</span>");
return newText;
Reply to this message...
Wayne King
The Replace method only replaces what is matched. Unmatched parts of the =
input string are not changed, so it appears that your regex is matching =
nothing. The problem could be because the backslash is a meta-character =
in C# literal strings. Try creating your regex this way, instead:
Regex
regexp =3D new
Regex
(
"\\b" + searchText + "\\b",
RegexOptions
.IgnoreCase);
Or:
Regex
regexp =3D new
Regex
(
@"\b" + searchText + @"\b",
RegexOptions
.IgnoreCase);
[Original message clipped]
-----Original Message-----
From: Rubric [mailto:
Click here to reveal e-mail address
]
Sent: Wednesday, April 03, 2002 10:39 AM
To: aspngregexp
Subject: [aspngregexp] Problem with Replace
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
I've written a Replace routine using the code below. The idea is to
replace the contents of the sourceText variable with a modified version
of the searchText variable.
The problem the code goes past the Replace command, but does not fire at
all. The returned newText variable is an unmodified version of
sourceText.
Is there something I'm missing?
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
//create a regular expression with the match string
Regex
regexp =3D new
Regex
("\b" + searchText + "\b",
RegexOptions
.IgnoreCase);
//replace the source text with this string
string newText =3D regexp.Replace(sourceText, "<span =
class=3D'highlight'>" +
searchText + "</span>");
return newText;
Reply to this message...
Rubric
That worked great -thanks.
The only issue is that the replaced string is a copy of the searchText
variable, which is a search string input by the user. The problem is
that if the user inputs "amsterdam" it will replace the string
"Amsterdam" verbatim.
Is there a way of retaining the case of the original string?
-----Original Message-----
From: Wayne King [mailto:
Click here to reveal e-mail address
]
Sent: 03 April 2002 19:52
To: aspngregexp
Subject: [aspngregexp] RE: Problem with Replace
The Replace method only replaces what is matched. Unmatched parts of the
input string are not changed, so it appears that your regex is matching
nothing. The problem could be because the backslash is a meta-character
in C# literal strings. Try creating your regex this way, instead:
Regex
regexp = new
Regex
(
"\\b" + searchText + "\\b",
RegexOptions
.IgnoreCase);
Or:
Regex
regexp = new
Regex
(
@"\b" + searchText + @"\b",
RegexOptions
.IgnoreCase);
[Original message clipped]
-----Original Message-----
From: Rubric [mailto:
Click here to reveal e-mail address
]
Sent: Wednesday, April 03, 2002 10:39 AM
To: aspngregexp
Subject: [aspngregexp] Problem with Replace
=============================================
I've written a Replace routine using the code below. The idea is to
replace the contents of the sourceText variable with a modified version
of the searchText variable.
The problem the code goes past the Replace command, but does not fire at
all. The returned newText variable is an unmodified version of
sourceText.
Is there something I'm missing?
=============================================
//create a regular expression with the match string
Regex
regexp = new
Regex
("\b" + searchText + "\b",
RegexOptions
.IgnoreCase);
//replace the source text with this string
string newText = regexp.Replace(sourceText, "<span class='highlight'>" +
searchText + "</span>");
return newText;
| [aspngregexp] member
Click here to reveal e-mail address
= YOUR ID
|
http://www.asplists.com/asplists/aspngregexp.asp
= JOIN/QUIT
|
http://www.asplists.com/search
= SEARCH Archives
Reply to this message...
Ryan Trudelle-Schwarz
Try using $1 in your replace code instead of the text the user gave.
Like:
Regex
.Replace(text, pattern, "<sometag>$1</sometag>");
Hth, Ryan
->-----Original Message-----
->From: Rubric [mailto:
Click here to reveal e-mail address
]
->
->That worked great -thanks.
->
->The only issue is that the replaced string is a copy of the searchText
->variable, which is a search string input by the user. The problem is
->that if the user inputs "amsterdam" it will replace the string
->"Amsterdam" verbatim.
->
->Is there a way of retaining the case of the original string?
->
->
->-----Original Message-----
->From: Wayne King [mailto:
Click here to reveal e-mail address
]
->
->The Replace method only replaces what is matched. Unmatched parts of
the
->input string are not changed, so it appears that your regex is
matching
->nothing. The problem could be because the backslash is a
meta-character
->in C# literal strings. Try creating your regex this way, instead:
->
Regex
regexp = new
Regex
(
-> "\\b" + searchText + "\\b",
->
RegexOptions
.IgnoreCase);
->Or:
->
Regex
regexp = new
Regex
(
-> @"\b" + searchText + @"\b",
->
RegexOptions
.IgnoreCase);
->
->
->> -WayneK
->> This posting is provided "AS IS" with no warranties, and confers no
->rights.
->
->
->-----Original Message-----
->From: Rubric [mailto:
Click here to reveal e-mail address
]
->
->=============================================
->
->I've written a Replace routine using the code below. The idea is to
->replace the contents of the sourceText variable with a modified
version
->of the searchText variable.
->The problem the code goes past the Replace command, but does not fire
at
->all. The returned newText variable is an unmodified version of
->sourceText.
->
->Is there something I'm missing?
->
->=============================================
->//create a regular expression with the match string
->Regex regexp = new
Regex
("\b" + searchText + "\b",
->RegexOptions.IgnoreCase);
->
->//replace the source text with this string
->string newText = regexp.Replace(sourceText, "<span class='highlight'>"
+
->searchText + "</span>");
->return newText;
---
[This E-mail scanned for viruses by Declude Virus]
Reply to this message...
Wayne King
Replace with what you match instead of with what you're searching for. =
For example:
Regex
regexp =3D new
Regex
(
@"\b" + searchText + @"\b",
RegexOptions
.IgnoreCase);
string newText =3D regexp.Replace(
sourceText,
"<span class=3D'highlight'>$0</span>");
$0 represents everything that was matched. It should retain the exact =
case of the input string.
-----Original Message-----
From: Rubric [mailto:
Click here to reveal e-mail address
]
Sent: Wednesday, April 03, 2002 3:50 PM
To: aspngregexp
Subject: [aspngregexp] RE: Problem with Replace
That worked great -thanks.
The only issue is that the replaced string is a copy of the searchText
variable, which is a search string input by the user. The problem is
that if the user inputs "amsterdam" it will replace the string
"Amsterdam" verbatim.
Is there a way of retaining the case of the original string?
-----Original Message-----
From: Wayne King [mailto:
Click here to reveal e-mail address
]=20
Sent: 03 April 2002 19:52
To: aspngregexp
Subject: [aspngregexp] RE: Problem with Replace
The Replace method only replaces what is matched. Unmatched parts of the
input string are not changed, so it appears that your regex is matching
nothing. The problem could be because the backslash is a meta-character
in C# literal strings. Try creating your regex this way, instead:
Regex
regexp =3D new
Regex
(
"\\b" + searchText + "\\b",
RegexOptions
.IgnoreCase);
Or:
Regex
regexp =3D new
Regex
(
@"\b" + searchText + @"\b",
RegexOptions
.IgnoreCase);
[Original message clipped]
-----Original Message-----
From: Rubric [mailto:
Click here to reveal e-mail address
]
Sent: Wednesday, April 03, 2002 10:39 AM
To: aspngregexp
Subject: [aspngregexp] Problem with Replace
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
I've written a Replace routine using the code below. The idea is to
replace the contents of the sourceText variable with a modified version
of the searchText variable.
The problem the code goes past the Replace command, but does not fire at
all. The returned newText variable is an unmodified version of
sourceText.
Is there something I'm missing?
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
//create a regular expression with the match string
Regex
regexp =3D new
Regex
("\b" + searchText + "\b",
RegexOptions
.IgnoreCase);
//replace the source text with this string
string newText =3D regexp.Replace(sourceText, "<span =
class=3D'highlight'>" +
searchText + "</span>");
return newText;
Reply to this message...
System.Text.RegularExpressions.Regex
System.Text.RegularExpressions.RegexOptions
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com