0

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.

1
Contributor
1
Reply
4
Views
5 Years
Discussion Span
Last Post by AleMonteiro
0

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 : "" ) );
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.