replace all letters except first letter with regexp?

T

teachpet

I'm trying to write a regular expression - with little success - for a
vba find and replace macro to replace all letters in a highlighted
text with an underscore except the first letter of each word.

Any help with the regular expression would be greatly appreciated!

Chris
 
T

Tony Jollans

There is nothing in Find and Replace that gives you the length of a found
term so the only way you could do this would be with multiple searches
looking for 4 letter words, 5 letter words, 6 letter words, etc.
 
G

Guest

Would not it be something like:

<([A-Za-z])([A-Za-z]{1;})

You'll need to use a correct separator in {1;}. In my system it's - ;
It may be a comma in yours.

Sergey
 
H

Helmut Weber

Hi teachpet,

have a look at this one:

Sub Test2a()
Dim rDcm As Range
Dim i As Long
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Highlight = True
.Text = "[A-Za-z]{1}[A-Za-z]{1;}"
.MatchWildcards = True
While .Execute
' rDcm.Select
For i = 2 To Len(rDcm)
rDcm.Characters(i) = "_"
Next
rDcm.Collapse Direction:=wdCollapseEnd
rDcm.End = ActiveDocument.Range.End
Wend
End With
End Sub
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

That should be

.Text = "[A-Za-z]{1}[A-Za-z]{1,}

for English systems.

Note the comma instead of the semicolon.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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