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: newlines in replacement string (ReplaceGlobal() does not process replacement strings containing newlines)
RegExp: newlines in replacement string [message #49518] Thu, 22 February 2018 10:40 Go to next message
norbert1968 is currently offline  norbert1968
Messages: 2
Registered: February 2018
Junior Member
I have a question regarding Upp::Regex. As I am not a very experienced programmer, my apologies if this is a stupid question.

In RegExp::ReplaceGlobal(String& t, const String& r, bool backref=false), replacements containing newlines are not processed. However, when using a Vector<String> as argument, this problem does not arise. Thus,

#include <Core/Core.h>
#include <plugin/pcre/Pcre.h>
using namespace Upp;

String s1 = "Too \n\n many \n\n\n   newlines!", s2 = s1;
RegExp reg;
reg.SetPattern("(\\s*\\n\\s*)"); // find (multiple) newlines and adjacent white space
String repl = "(\n)";            // trim to single newline
reg.ReplaceGlobal(s1, repl);     // doesn't work as expected, but deletes any match
DUMP (s1);

reg.ReplaceGlobal(s2, Vector<String>({"\n"}));    // does work
DUMP (s2);


yields the following output:
s1 = Toomanynewlines!
s2 = Too
many
newlines!


The reason seems to be that RegExp::Make_rv(const String&) uses a regex itself to identify text within parenthesis, and this regex apparently does not use the "dot matches newline" option. Is this behaviour by design? I would prefer the s1 example to work as expected.
Re: RegExp: newlines in replacement string [message #49530 is a reply to message #49518] Sat, 24 February 2018 18:18 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12122
Registered: November 2005
Ultimate Member
I think this is just accidental.

I have fixed it this way:


Vector<String> RegExp::Make_rv(const String& r)
{
	RegExp reg("\\(((\r|\n|.)*?)\\)");
	
	Vector<String> rv;
	
	while(reg.GlobalMatch(r)){
		rv.Add(reg.GetString(0));
	}
	
	return rv;
}

Re: RegExp: newlines in replacement string [message #49531 is a reply to message #49530] Sat, 24 February 2018 19:12 Go to previous message
norbert1968 is currently offline  norbert1968
Messages: 2
Registered: February 2018
Junior Member
Perfect! Thank you! Smile
Previous Topic: Filtering streams for bz2
Next Topic: StaticText : Add missing GetData/SetData overloads
Goto Forum:
  


Current Time: Wed Dec 11 02:24:12 CET 2019

Total time taken to generate the page: 0.01402 seconds