codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Regular Expression Query Replace -- Twice Wrongly  TAS at 10:52 on Friday, July 03, 2009
 

Dear All,

I am doing a XML using C#. I wrote a loop to run foreign and then ordinance tag. While foreign tag running, it performs correctly. Afterwards it run ordinance tag, it gives wrong answer.

The sentence is

<num>7</num><p><text>Indian Penal Code (UK), applicable in Malaysia Indian Penal Code by virtue of the Courts of Judicature Act 1964 s 24(b).</text></p>

It should be replaced properly as

<num>7</num><p><text><strf tblxtract="foreign"><strfact>Indian Penal Code (UK)</strfact></strf>, applicable in Malaysia <strf tblxtract="ordinance"><strfact>Indian Penal Code</strfact></strf> by virtue of the Courts of Judicature Act 1964 s 24(b).</text></p>


Foreign Loop coding lines:

string strContent = "<num>7</num><p><text>Indian Penal Code (UK), applicable in Malaysia Indian Penal Code by virtue of the Courts of Judicature Act 1964 s 24(b).</text></p>";
string strSearch = "Indian Penal Code (UK)"

string strPattern = "([^<strfact])" + strSearch.Replace("(", "\\(").Replace(")", "\\)").Substring(0);

strContent = Regex.Replace(strContent, strPattern , "$1" + "<strf txlxtract="foreign"><strfact>" + strSearch + "</strfact></strf>");

MessageBox.Show(strContent);

Ordinance Loop coding lines:

string strContent = "<num>7</num><p><text>Indian Penal Code (UK), applicable in Malaysia Indian Penal Code by virtue of the Courts of Judicature Act 1964 s 24(b).</text></p>";
string strSearch = "Indian Penal Code"

string strPattern = "([^<strfact])" + strSearch.Replace("(", "\\(").Replace(")", "\\)").Substring(0);

strContent = Regex.Replace(strContent, strPattern , "$1" + "<strf txlxtract="ordinance"><strfact>" + strSearch + "</strfact></strf>");

MessageBox.Show(strContent);



It gives Final Result wrongly:

<num>7</num><p><text><strf tblxtract="foreign"><strfact><strf tblxtract="ordinance" id="TBC"><strfact>Indian Penal Code</strfact></strf> (UK)</strfact></strf> s 20(4), applicable in Malaysia <strf tblxtract="ordinance" id="TBC"><strfact>Indian Penal Code</strfact></strf> by virtue of the Courts of Judicature Act 1964.</text></p>


I dont know where i made a mistake "Indian Penal Code" has replaced twice. Pls help me









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Regular Expression Query Replace -- Twice Wrongly
•  Processing MS Office documents with Java
•  Re: help!
•  Re: Can javascript preload swf files?
•  ADODB.Recordset error (0x800A0CB3)
•  Need Your Inputs About Level Editor Interface
•  `section` is an unexpected token. Expecting white space. Line 1, position 137.
•  Javascript rollover toggle help please!
•  help monitoting cpu load on windows machine


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009