We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Range.Replace Regex


I have a problem in Range.Replace()

I try to replace tempsStr string in my Section and tempsStr can contain any special caracter. I don’t know how to create my Regex expression.

Dim s As Section = CType(node, Section)

s.Range.Replace(tempsStr, String.Empty, False, False)


dim regex as string = ???

Dim myRegex as Regex = new Regex(regex)

Thanks for the help,

<span style=“font-family: “Courier New”; color: windowtext;” lang=“EN-US”>Emilia


<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. As mentioned in the documentation captured and replacement strings cannot contain special characters:


Could you please provide more information about your goals? Maybe there is a better way to achieve what you need.

Best regards.

How can i do to replace or remove a part of the section’s text that can contain the special caracters?
For example i have the section with the range.text= “aaa{start}hrflsdqfmdqf{end}aa{start}aaaaaaaaaaajhkxhwfaaa{end}aaafjdfjdsfjfjfjfjfjfjfjfjfj"
I need to remove ou replace the parts of the text between {start} end {end} but i have the specials caracters inside.

mysection.Range.Text.Replace(str_to_replace,”") doesn’t work
mysection.Range.Replace(tempsStr, String.Empty, False, False) doesn’t work because of special caracters

I thought that i can use regex as following

Dim str_regex = tempsStr.ToString.Replace(vbCrLf, “\s”).Replace(vbCr, “\s”).etc
Dim reg As Regex = New Regex(str_regex)
mysection.Range.Replace(reg, String.Empty)

help !!!

Best regards.



<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. There is no direct way to achieve this, but it is possible to achieve. I created a simple code example, which demonstrates the technique, you can use to remove content between such placeholders:

Public Sub Test001()

Dim doc As Document = New Document("C:\Temp\in.doc")

Dim startFinder As PlaceholderFinder = New PlaceholderFinder(doc, True, "{start}")

Dim endFinder As PlaceholderFinder = New PlaceholderFinder(doc, True, "{end}")

Dim startNode As Node = startFinder.FindPlaceholder()

Dim endNode As Node = endFinder.FindPlaceholder()

RemoveSequence(startNode, endNode)




End Sub


''' Remove all nodes between start and end nodes, except start and end nodes


''' The start node

''' The end node


Private Sub RemoveSequence(ByVal startNode As Node, ByVal endNode As Node)

Dim curNode As Node = startNode.NextPreOrder(startNode.Document)

While ((curNode IsNot Nothing) And (Not curNode.Equals(endNode)))

'Move to next node

Dim nextNode As Node = curNode.NextPreOrder(startNode.Document)

'Check whether current contains end node

If (curNode.IsComposite) Then

If (Not DirectCast(curNode, CompositeNode).GetChildNodes(NodeType.Any, True).Contains(endNode) And _

Not DirectCast(curNode, CompositeNode).GetChildNodes(NodeType.Any, True).Contains(startNode)) Then

nextNode = curNode.NextSibling


End If



End If

curNode = nextNode

End While

End Sub

Private Class PlaceholderFinder


''' Creates new instance of Placeholderfinder


''' Document where we ned to find placeholder.

''' Set this flag is captured strign is start fo region.

''' Placeholder.


Public Sub New(ByVal doc As Document, ByVal isStart As Boolean, ByVal placeHolder As String)

mDoc = doc

mIsStart = isStart

mPlaceHolder = placeHolder

End Sub

Public Function FindPlaceholder() As Node

Dim myRegex As Regex = New Regex(mPlaceHolder)

mDoc.Range.Replace(myRegex, New ReplaceEvaluator(AddressOf ReplaceEvaluatorFindPlaceholder), True)

Return mPlaceholderNode

End Function


''' This method is called by the Aspose.Words find and replace engine for each match.

''' This method initialize placeholders node if find some.


Private Function ReplaceEvaluatorFindPlaceholder(ByVal sender As Object, ByVal e As ReplaceEvaluatorArgs) As ReplaceAction

' This is a Run node that contains either the beginning or the complete match.

Dim currentNode As Node = e.MatchNode

' The first (and may be the only) run can contain text before the match,

' in this case it is necessary to split the run.

If e.MatchOffset > 0 Then

currentNode = SplitRun(CType(currentNode, Run), e.MatchOffset)

End If

' This array is used to store all nodes of the match.

Dim runs As ArrayList = New ArrayList()

' Find all runs that contain parts of the match string.

Dim remainingLength As Integer = e.Match.Value.Length

Do While (remainingLength > 0) AndAlso (currentNode IsNot Nothing) AndAlso (currentNode.GetText().Length <= remainingLength)


remainingLength = remainingLength - currentNode.GetText().Length

' Select the next Run node.

' Have to loop because there could be other nodes such as BookmarkStart etc.


currentNode = currentNode.NextSibling

Loop While (currentNode IsNot Nothing) AndAlso (currentNode.NodeType <> NodeType.Run)


' Split the last run that contains the match if there is any text left.

If (currentNode IsNot Nothing) AndAlso (remainingLength > 0) Then

SplitRun(CType(currentNode, Run), remainingLength)


End If

If (mIsStart) Then

mPlaceholderNode = CType(runs.Item(0), Node)


mPlaceholderNode = CType(runs.Item(runs.Count - 1), Node)

End If

' Signal to the replace engine to stop searching

Return ReplaceAction.Stop

End Function


''' Splits text of the specified run into two runs.

''' Inserts the new run just after the specified run.


Private Shared Function SplitRun(ByVal run As Run, ByVal position As Integer) As Run

Dim afterRun As Run = CType(run.Clone(True), Run)

afterRun.Text = run.Text.Substring(position)

run.Text = run.Text.Substring(0, position)

run.ParentNode.InsertAfter(afterRun, run)

Return afterRun

End Function

Private mPlaceholderNode As Node

Private mDoc As Document

Private mIsStart As Boolean

Private mPlaceHolder As String

End Class

Hope this helps.

Best regards.