Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Library : Other (not classified elsewhere) » RegExp::ReplaceGlobal() is buggy
RegExp::ReplaceGlobal() is buggy [message #51891] Wed, 12 June 2019 00:47 Go to previous message
zsolt is currently offline  zsolt
Messages: 598
Registered: December 2005
Location: Budapest, Hungary
Contributor
I did not investigate it, but multiple replaces are not working well:
		String res8;

		res8 = "ab<a>ZZ</a>";
		RegExp("(<\\s*a[^>]*>(.*?)<\\s*/\\s*a>)", RegExp::UNICODE|RegExp::CASELESS)
			.ReplaceGlobal(res8, "()");
		CHECK_EQUAL("ab", ~res8); //this is OK

		res8 = "12<a>ZZ</a>34<a>vv</a>56";
		RegExp("(<\\s*a[^>]*>(.*?)<\\s*/\\s*a>)", RegExp::UNICODE|RegExp::CASELESS)
			.ReplaceGlobal(res8, "()");
		CHECK_EQUAL("123456", ~res8); //buggy



The ReplaceGlobal implementation is too hard for me, so I created a very simple but working method for myself to be able to use regular expressions to multiple replaces:
String RegExp::ReplaceAll(const String& s, const String& r)
{
	String res;
	int iPosStart;
	int iPosAfterEnd;
	int iFromCopyToRes = 0;
	while(GlobalMatch(s)){
		GetMatchPos(0, iPosStart, iPosAfterEnd);
		//String matched = s.Mid(iPosStart, iPosAfterEnd-iPosStart);//debug
		res << s.Mid(iFromCopyToRes, iPosStart-iFromCopyToRes);
		res << r;
		iFromCopyToRes = iPosAfterEnd;
	}
	if(iFromCopyToRes < s.GetCount()){
		res << s.Mid(iFromCopyToRes, s.GetCount()-iFromCopyToRes);
	}
	return res;
}


Test code:
		String res8;
		res8 = "12<a>ZZ</a>34<a>vv</a>56";
		res8 = RegExp("(<\\s*a[^>]*>(.*?)<\\s*/\\s*a>)", RegExp::UNICODE|RegExp::CASELESS)
			.ReplaceAll(res8, "");
		CHECK_EQUAL("123456", ~res8);
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: [FEATURE + patch]WriteClipboardHTML and HtmlToText functions
Next Topic: Setting right align in ArrayCtrl does not work
Goto Forum:
  


Current Time: Fri Dec 06 04:59:18 CET 2019

Total time taken to generate the page: 0.01719 seconds