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 next message
zsolt is currently offline  zsolt
Messages: 584
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);
Re: RegExp::ReplaceGlobal() is buggy [message #51895 is a reply to message #51891] Wed, 12 June 2019 10:10 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12016
Registered: November 2005
Ultimate Member
Thanks. Before fixing RegExp, have you considered using https://en.cppreference.com/w/cpp/regex ?

Mirek
Re: RegExp::ReplaceGlobal() is buggy [message #51896 is a reply to message #51895] Wed, 12 June 2019 13:02 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 584
Registered: December 2005
Location: Budapest, Hungary
Contributor
Thanks, I didn't know, that it is in the standard now.
Btw, Using RegEx class is much more easy.
Re: RegExp::ReplaceGlobal() is buggy [message #51897 is a reply to message #51896] Wed, 12 June 2019 13:38 Go to previous message
mirek is currently offline  mirek
Messages: 12016
Registered: November 2005
Ultimate Member
zsolt wrote on Wed, 12 June 2019 13:02
Thanks, I didn't know, that it is in the standard now.
Btw, Using RegEx class is much more easy.


Well, I hoped that somebody would try and tell me whether to keep maintaining RegExp... "0

Mirek
Previous Topic: [FEATURE + patch]WriteClipboardHTML and HtmlToText functions
Next Topic: Setting right align in ArrayCtrl does not work
Goto Forum:
  


Current Time: Mon Sep 16 14:07:15 CEST 2019

Total time taken to generate the page: 0.01336 seconds