Deleting between tags

J

julian_m

I want to delete whatever text between certain "tags" (note that I need
to remove html css comments as well as PHP comments)

so I have the following code

Sub EliminarCSSComments()

Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "/#*#/"
.Forward = True
.Wrap = wdFindStop 'para cuando se llega al final del documento
.MatchWildcards = True
Do While .Execute
oRng.Select
MsgBox "encontro"
oRng.Delete
Loop
End With

this works quite well if I wanted to delete text between /# and #/

see for instance this example

if I have
/# this should be removed #/
/# this should /be re#moved #/

both lines will be deleted.

but in the other hand, when I want to delete the text between /* and */
(wich is actually what I need), the code won't work as expected

for instance, if I have

/* this should be removed */
/* this sho/uld be r*emoved */

the result is

uld be r*emoved */

note that I tryied with the following text to search

.Text = "/" + Chr(42) + "*" + Chr(42) + "/"
'.Text = "/* */"
'.Text = "/*/"
'.Text = "/\*?{1,}\*/"
'.Text = "/**/"
'.Text = "</*>*<*/>"
'.Text = "/\*?{1,}\*/"

the problem seems to be that i need to search for asterisks(*), which
are seen by Word as wildcards...

any hint?

sdos - jm
 
T

Tony Jollans

Hi Julian,

You are correct in your assumption about the asterisks. To find an actual
asterisk you must 'escape' it by preceding it with a backslash - so looking
for "/\*" (with wildcards) finds "/*" rather than "/anything"
 
J

Jay Freedman

Because an asterisk has a special meaning in search expressions, each
'real' asterisk must be preceded by a backslash. The search expression
should be

.Text = "/\*(*)\*/"

The first and third asterisks will match actual asterisk characters,
and the second asterisk will match everything between them. It doesn't
seem to me that the parentheses should be necessary if you aren't
using the \1 group in the replacement, but the search doesn't find
anything if they're omitted.

By the way, if you can do without the message box, you can speed up
you macro by rewriting it this way:

Sub EliminarCSSComments()
Dim oRng As Range
Set oRng = ActiveDocument.Content
With oRng.Find
.ClearFormatting
.Text = "/#*#/"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
J

julian_m

Jay said:
Because an asterisk has a special meaning in search expressions, each
'real' asterisk must be preceded by a backslash. The search expression
should be

.Text = "/\*(*)\*/"

It works like a charm, even though I ask myself what means the
parentheses there...
I now understand how to search for special characters.
By the way, if you can do without the message box, you can speed up
you macro by rewriting it this way:

Right. I was using the message box just to know what was happening
with the code ; )


Thank you very much.

saludos - jm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top