find and replace using wildcard characters

F

fjfvan

How do I do a search and replace using wildcard characters? I know that I
have to select Use wildcards in the search and replace dialog window. For
instance I want to replace all instances of words enclosed using single
quotes with double quotes. I put the following in the find entry field: '*'.
Word retrieves the words in single quotes, but when I try to replace it with
the following in the replace entry field "*", it replaces the string with
exactly that ("*"). I don't know how to make the replace use the wildcard
characters as well.
 
K

Klaus Linke

fjfvan said:
How do I do a search and replace using wildcard characters?
I know that I have to select Use wildcards in the search and replace
dialog window. For instance I want to replace all instances of words
enclosed using single quotes with double quotes. I put the following
in the find entry field: '*'.
Word retrieves the words in single quotes, but when I try to replace it
with the following in the replace entry field "*", it replaces the string
with exactly that ("*"). I don't know how to make the replace use the
wildcard characters as well.


You have a couple of things right, but it does not quite fit together yet...

* does match any text, and since you want to match anything between single
quotes, that's a good start.
You can put stuff in (parentheses) so that you can then re-use those
sub-expressions within the (parentheses) in "Replace with", and throw away
stuff that is outside.

So you might use
Find what: '(*)'

What's missing is how to re-use the stuff in the bracket in "Replace
with"...
That is done with \1 ...

So you could use
Replace with: "\1"

For safety, I'd use say
Find what: <'(*)'>

That introduces the jokers < and > which only match text at the beginning
and at the end of a word.

The reason: Apostrophes appear all over the place in English, so it makes
sense to lower the changes you match something that is not actually a quote.

For the same reason, I avoid the * wildcard as too risky, and use say
[!^13]@ instead, which matches any text too, but never matches across
paragraphs (... ^13 being the code of the paragraph mark).

For figuring out the above, and for more info, see for example
http://word.mvps.org/faQs/General/UsingWildcards.htm

But above that, experiment whenever you have something that looks like it
might be done better with a wildcard replacement.

Regards,
Klaus
 
F

fjfvan

Dear Klaus:

Thank you so much! You are a lifesaver. This helped enormously.

FF

Klaus Linke said:
fjfvan said:
How do I do a search and replace using wildcard characters?
I know that I have to select Use wildcards in the search and replace
dialog window. For instance I want to replace all instances of words
enclosed using single quotes with double quotes. I put the following
in the find entry field: '*'.
Word retrieves the words in single quotes, but when I try to replace it
with the following in the replace entry field "*", it replaces the string
with exactly that ("*"). I don't know how to make the replace use the
wildcard characters as well.


You have a couple of things right, but it does not quite fit together yet...

* does match any text, and since you want to match anything between single
quotes, that's a good start.
You can put stuff in (parentheses) so that you can then re-use those
sub-expressions within the (parentheses) in "Replace with", and throw away
stuff that is outside.

So you might use
Find what: '(*)'

What's missing is how to re-use the stuff in the bracket in "Replace
with"...
That is done with \1 ...

So you could use
Replace with: "\1"

For safety, I'd use say
Find what: <'(*)'>

That introduces the jokers < and > which only match text at the beginning
and at the end of a word.

The reason: Apostrophes appear all over the place in English, so it makes
sense to lower the changes you match something that is not actually a quote.

For the same reason, I avoid the * wildcard as too risky, and use say
[!^13]@ instead, which matches any text too, but never matches across
paragraphs (... ^13 being the code of the paragraph mark).

For figuring out the above, and for more info, see for example
http://word.mvps.org/faQs/General/UsingWildcards.htm

But above that, experiment whenever you have something that looks like it
might be done better with a wildcard replacement.

Regards,
Klaus
 

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