Hi everybody.

I'm trying to insert an indented code inside another indented code, using the first indentation as base for the second. I mean, the second indentation must begin at the end of the first.

The indentation i got working right, but it is adding some undesired line breaks, that i need to avoid.

Here is the example for the code, i won't post the actual code because the strings are build dynamically and some i got from text files. So i split it up and put it together for easy understanding.

//-------------------------------------------------
// STRING TO REPLACE WITH
//-------------------------------------------------
<div class="form-item">
	<div class="form-item-label inline" ><span></span></div>
	<input type="text" value="" >
</div>

//-------------------------------------------------
// STRING WHERE REPLACING IN
//-------------------------------------------------
<div id="formFiltro" class="form-filtro">
	<fieldset>
		<legend>Filtros</legend>
		
		<-'REPLACE_HERE'->

	</fieldset>
</div>


//-------------------------------------------------
// DESIRED RESULT
//-------------------------------------------------

<div id="formFiltro" class="form-filtro">
	<fieldset>
		<legend>Filtros</legend>
		
		<div class="form-item">
			<div class="form-item-label inline" ><span></span></div>
			<input type="text" value="" >
		</div>

	</fieldset>
</div>


//-------------------------------------------------
// MY ATTEMPT
//-------------------------------------------------

// newCode = string to replace with
// code = string where replacing

string pattern = "\r\n|\r|\n"; // Replace line breaks with "$1" to be used on the second replace

newCode = Regex.Replace( newCode, pattern, @"$1" );

pattern = @"([\s\t]*)(<-'REPLACE_HERE'->)"; //Copy default identation for new code while keeping "<-'REPLACE_HERE'->" on the original position

code = Regex.Replace(codigo, pattern, String.Format(@"$1$2$1{0}", newCode));


//-------------------------------------------------
// WHAT I GOT
//-------------------------------------------------

<div id="formFiltro" class="form-filtro">
	<fieldset>
		<legend>Filtros</legend>
		
		<div class="form-item">
		
			<div class="form-item-label inline" ><span></span></div>
			
			<input type="text" value="" >
			
		</div>

	</fieldset>
</div>

Thanks in advance.

Solved:

string flag = String.Format( @"<\?'{0}'\?>", pFlag.ToUpper() );
            pCodigo = Regex.Replace( pCodigo, @"(\r?\n)", "$1$$1", RegexOptions.Multiline );
            string pattern = String.Format( @"([ \t]*)({0})", flag );
            _strCodigo = Regex.Replace( codigo, pattern, String.Format( @"$1{0}{1}$1$2", pCodigo, pLineBreak ? Environment.NewLine : "" ) );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.