.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification