AND and OR in Find and Replace

K

kuku1375

Is it possible to use operators like AND or OR in Word's Find and Search,
e.g. to find all instances of "John Smith" and "J. Smith" in one search?
 
J

Jezebel

If you check the 'Use wildcards' checkbox, the find and replace mechansim is
a version of Regular Expressions, which you will find documented in numerous
places on the net. (For no obvious reason, Microsoft doesn't quite follow
the standard, but close enough for most purposes.)

Regular expressions don't use AND or OR as such, but they do provide
equivalent mechanisms: eg, [xyz] means 'x' or 'y' or 'z'; [0-9] means any
single digit. For your example, searching for: J[! ]{1,} Smith will match
"J plus one or more non-space characters plus space plus Smith". IE, both of
your examples plus any number of variants like "Josephine-Keller Smith" etc.
Note that it doesn't match "J Smith". (One of the unfortunate gaps in MS's
regular expressions is 'Zero or more'.)

You could also use J*Smith which matches "J plus zero or more characters
plus Smith" which might be acceptable depending on your source data; but it
would also match strings like "John Brown and Michael Smith".
 
K

kuku1375

Thank you! I read Help before, but their language of "One of the specified
characters
Type [ ] " was not quite clear for me. I wish they used Regular Expressions!

Jezebel said:
If you check the 'Use wildcards' checkbox, the find and replace mechansim is
a version of Regular Expressions, which you will find documented in numerous
places on the net. (For no obvious reason, Microsoft doesn't quite follow
the standard, but close enough for most purposes.)

Regular expressions don't use AND or OR as such, but they do provide
equivalent mechanisms: eg, [xyz] means 'x' or 'y' or 'z'; [0-9] means any
single digit. For your example, searching for: J[! ]{1,} Smith will match
"J plus one or more non-space characters plus space plus Smith". IE, both of
your examples plus any number of variants like "Josephine-Keller Smith" etc.
Note that it doesn't match "J Smith". (One of the unfortunate gaps in MS's
regular expressions is 'Zero or more'.)

You could also use J*Smith which matches "J plus zero or more characters
plus Smith" which might be acceptable depending on your source data; but it
would also match strings like "John Brown and Michael Smith".



kuku1375 said:
Is it possible to use operators like AND or OR in Word's Find and Search,
e.g. to find all instances of "John Smith" and "J. Smith" in one search?
 
G

Greg Maxey

Offered only as a novelty:

Another user and I where discussing using OR in a find text problem. The
discussion turned to a very real human situation where the house is locked
and the car is locked. A known spare house key is in the car and a known
spare car hey is in the house. There are other keys somewhere to be found.
Obviously the problem is resolved and the search can end when either a house
or car key is found.


Cheers


Sub HelmutsDilemma()
Dim aWord As Range
Dim myStr As String
For Each aWord In ActiveDocument.Range.Words
myStr = aWord
If InStrRev(myStr, " ") = Len(myStr) Then
myStr = Left(myStr, (Len(myStr) - 1))
End If
Select Case myStr
Case Is = "car", "house"
If aWord.Next(Unit:=wdWord, Count:=1) = "keys " Or _
aWord.Next(Unit:=wdWord, Count:=1) = "keys" Then
With aWord
.MoveEnd Unit:=wdWord, Count:=1
.Select
End With
Exit Sub
End If
Case Else
'Do Nothing
End Select
Next
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
If you check the 'Use wildcards' checkbox, the find and replace
mechansim is a version of Regular Expressions, which you will find
documented in numerous places on the net. (For no obvious reason,
Microsoft doesn't quite follow the standard, but close enough for
most purposes.)
Regular expressions don't use AND or OR as such, but they do provide
equivalent mechanisms: eg, [xyz] means 'x' or 'y' or 'z'; [0-9] means
any single digit. For your example, searching for: J[! ]{1,} Smith will
match "J plus one or more non-space characters plus space plus
Smith". IE, both of your examples plus any number of variants like
"Josephine-Keller Smith" etc. Note that it doesn't match "J Smith".
(One of the unfortunate gaps in MS's regular expressions is 'Zero or
more'.)
You could also use J*Smith which matches "J plus zero or more
characters plus Smith" which might be acceptable depending on your
source data; but it would also match strings like "John Brown and
Michael Smith".


kuku1375 said:
Is it possible to use operators like AND or OR in Word's Find and
Search, e.g. to find all instances of "John Smith" and "J. Smith" in
one search?
 
G

Graham Mayor

Maybe http://www.gmayor.com/replace_using_wildcards.htm will help?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Thank you! I read Help before, but their language of "One of the
specified characters
Type [ ] " was not quite clear for me. I wish they used Regular
Expressions!

Jezebel said:
If you check the 'Use wildcards' checkbox, the find and replace
mechansim is a version of Regular Expressions, which you will find
documented in numerous places on the net. (For no obvious reason,
Microsoft doesn't quite follow the standard, but close enough for
most purposes.)

Regular expressions don't use AND or OR as such, but they do provide
equivalent mechanisms: eg, [xyz] means 'x' or 'y' or 'z'; [0-9]
means any single digit. For your example, searching for: J[! ]{1,}
Smith will match "J plus one or more non-space characters plus
space plus Smith". IE, both of your examples plus any number of
variants like "Josephine-Keller Smith" etc. Note that it doesn't
match "J Smith". (One of the unfortunate gaps in MS's regular
expressions is 'Zero or more'.)

You could also use J*Smith which matches "J plus zero or more
characters plus Smith" which might be acceptable depending on your
source data; but it would also match strings like "John Brown and
Michael Smith".



kuku1375 said:
Is it possible to use operators like AND or OR in Word's Find and
Search, e.g. to find all instances of "John Smith" and "J. Smith"
in one search?
 
K

kuku1375

Thank you all for the valuable information you shared. However there is
another problem related with search using wildcards. I tried an example from
Help i.e. 10{1,3} to find "10", "100", and "1000" which worked fine. However
a friend got a problem - see the skrinshot:
http://img513.imageshack.us/my.php?image=err0gs.jpg
What causes the error?

Greg Maxey said:
Offered only as a novelty:

Another user and I where discussing using OR in a find text problem. The
discussion turned to a very real human situation where the house is locked
and the car is locked. A known spare house key is in the car and a known
spare car hey is in the house. There are other keys somewhere to be found.
Obviously the problem is resolved and the search can end when either a house
or car key is found.


Cheers


Sub HelmutsDilemma()
Dim aWord As Range
Dim myStr As String
For Each aWord In ActiveDocument.Range.Words
myStr = aWord
If InStrRev(myStr, " ") = Len(myStr) Then
myStr = Left(myStr, (Len(myStr) - 1))
End If
Select Case myStr
Case Is = "car", "house"
If aWord.Next(Unit:=wdWord, Count:=1) = "keys " Or _
aWord.Next(Unit:=wdWord, Count:=1) = "keys" Then
With aWord
.MoveEnd Unit:=wdWord, Count:=1
.Select
End With
Exit Sub
End If
Case Else
'Do Nothing
End Select
Next
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
If you check the 'Use wildcards' checkbox, the find and replace
mechansim is a version of Regular Expressions, which you will find
documented in numerous places on the net. (For no obvious reason,
Microsoft doesn't quite follow the standard, but close enough for
most purposes.)
Regular expressions don't use AND or OR as such, but they do provide
equivalent mechanisms: eg, [xyz] means 'x' or 'y' or 'z'; [0-9] means
any single digit. For your example, searching for: J[! ]{1,} Smith will
match "J plus one or more non-space characters plus space plus
Smith". IE, both of your examples plus any number of variants like
"Josephine-Keller Smith" etc. Note that it doesn't match "J Smith".
(One of the unfortunate gaps in MS's regular expressions is 'Zero or
more'.)
You could also use J*Smith which matches "J plus zero or more
characters plus Smith" which might be acceptable depending on your
source data; but it would also match strings like "John Brown and
Michael Smith".


kuku1375 said:
Is it possible to use operators like AND or OR in Word's Find and
Search, e.g. to find all instances of "John Smith" and "J. Smith" in
one search?
 
K

kuku1375

I found the cause of the error. Regional settings were not English, so the
formula had to use semicolon rather than comma.

kuku1375 said:
Thank you all for the valuable information you shared. However there is
another problem related with search using wildcards. I tried an example from
Help i.e. 10{1,3} to find "10", "100", and "1000" which worked fine. However
a friend got a problem - see the skrinshot:
http://img513.imageshack.us/my.php?image=err0gs.jpg
What causes the error?

Greg Maxey said:
Offered only as a novelty:

Another user and I where discussing using OR in a find text problem. The
discussion turned to a very real human situation where the house is locked
and the car is locked. A known spare house key is in the car and a known
spare car hey is in the house. There are other keys somewhere to be found.
Obviously the problem is resolved and the search can end when either a house
or car key is found.


Cheers


Sub HelmutsDilemma()
Dim aWord As Range
Dim myStr As String
For Each aWord In ActiveDocument.Range.Words
myStr = aWord
If InStrRev(myStr, " ") = Len(myStr) Then
myStr = Left(myStr, (Len(myStr) - 1))
End If
Select Case myStr
Case Is = "car", "house"
If aWord.Next(Unit:=wdWord, Count:=1) = "keys " Or _
aWord.Next(Unit:=wdWord, Count:=1) = "keys" Then
With aWord
.MoveEnd Unit:=wdWord, Count:=1
.Select
End With
Exit Sub
End If
Case Else
'Do Nothing
End Select
Next
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
If you check the 'Use wildcards' checkbox, the find and replace
mechansim is a version of Regular Expressions, which you will find
documented in numerous places on the net. (For no obvious reason,
Microsoft doesn't quite follow the standard, but close enough for
most purposes.)
Regular expressions don't use AND or OR as such, but they do provide
equivalent mechanisms: eg, [xyz] means 'x' or 'y' or 'z'; [0-9] means
any single digit. For your example, searching for: J[! ]{1,} Smith will
match "J plus one or more non-space characters plus space plus
Smith". IE, both of your examples plus any number of variants like
"Josephine-Keller Smith" etc. Note that it doesn't match "J Smith".
(One of the unfortunate gaps in MS's regular expressions is 'Zero or
more'.)
You could also use J*Smith which matches "J plus zero or more
characters plus Smith" which might be acceptable depending on your
source data; but it would also match strings like "John Brown and
Michael Smith".


"kuku1375" <my_sweet_email(at)yahoo.com> wrote in message
Is it possible to use operators like AND or OR in Word's Find and
Search, e.g. to find all instances of "John Smith" and "J. Smith" in
one search?
 

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