PDA

View Full Version : Vba Remove characters between two delimiters



mattthatsme
11-12-2010, 07:09 AM
Hi I'm trying to find a solution to remove a variable length string between
two delimiters.

At the end of my string i have: \variable-length\ and i need to remove the
two delimiters \ and the variable length string in between them.

I know i can do mysting= Left(mysting, Len(mysting) - 1) to get rid of the
last delimiter
then remove everything to the right of the next delimiter
and then do the first part again....

I'm sure there must be a better solution and i've google'd loads but can't
find anything.

Any help would be great.

Thanks

John Wilson
11-12-2010, 10:43 AM
Presuming you need to remove the delimiters also try this (in this case the delimiters are < and >

Sub use_regex()
Dim regX As Object
Dim osld As Slide
Dim oshp As Shape
Dim strInput As String
Dim b_found As Boolean

Set regX = CreateObject("vbscript.regexp")
With regX
.Global = True
'the delimiters here are < and > change pattern as required
.Pattern = "<(\w+)>"
End With
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If oshp.HasTextFrame Then
If oshp.TextFrame.HasText Then
strInput = oshp.TextFrame.TextRange.Text
b_found = regX.Test(strInput)
If b_found = True Then
strInput = regX.Replace(strInput, "")
oshp.TextFrame.TextRange = strInput
End If
End If
End If

Next oshp
Next osld
End Sub

Sycdan
11-12-2010, 10:57 AM
You could use a regular expression:

Function unDelim(myString)

Dim re As Object, result As Object

Set re = CreateObject("vbscript.regexp")
With re
.MultiLine = False
.Global = False
.IgnoreCase = True
.Pattern = "(\\)([^\\]*)(\\)"
End With

Set result = re.Execute(myString)

Debug.Print "Delim1", result(0).submatches(0)
Debug.Print "String", result(0).submatches(1)
Debug.Print "Delim2", result(0).submatches(2)

End Function

The pattern would need to be modified if your delimiters are not always backslashes.

mattthatsme
11-12-2010, 11:46 AM
John and Sycdan thanks very much for the prompt response. All Works perfectly a great help cheers