Reply
 
Thread Tools Display Modes
  #1   Report Post  
kaston kaston is offline
Junior Member
 
Posts: 0
Default search for strings separated by 1-n characters?

i want to do a regular expression search in a word doc that will let me find
two strings separated by say 50 characters of any type or less, assuming carriage returns are considered characters. the idea is to be able to search a document for 2 words that are separated by other unknown words but that are "close" to each other.

so if i want to find the strings "blah" and "ya"
i tried the expression blah?{1,50}ya hoping that the ?{1,50} would specify
1-50 single characters of any type. but this finds blah and ya even if they
are separated by many pages (hundreds of characters). what am i doing wrong?

thanks,
kaston
  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default search for strings separated by 1-n characters?

You could do it with a macro containing the following code:

Dim Range1 As Range, Range2 As Range
Dim Word1 As String, Word2 As String, Spacing As Long, MaxSpace As Long
Word1 = InputBox("Enter the first word.")
Word2 = InputBox("Enter the second word.")
MaxSpace = Val(InputBox("Enter the maximum number of characters between the
words."))
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:=Word1, Forward:=True, _
MatchWildcards:=False, Wrap:=wdFindStop, MatchCase:=False) = True
Set Range1 = Selection.Range
Set Range2 = Range1.Duplicate
Range2.End = ActiveDocument.Range.End
Spacing = InStr(Range2, Word2)
If Spacing 0 And Spacing - Len(Word1) MaxSpace Then
Range1.End = Range1.Start + Spacing + Len(Word2) - 1
Range1.Select
MsgBox "OK"
End If
Selection.Collapse wdCollapseEnd
Selection.MoveRight wdCharacter, 1
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
"kaston" wrote in message
...

i want to do a regular expression search in a word doc that will let me
find
two strings separated by say 50 characters of any type or less,
assuming carriage returns are considered characters. the idea is to be
able to search a document for 2 words that are separated by other
unknown words but that are "close" to each other.

so if i want to find the strings "blah" and "ya"
i tried the expression blah?{1,50}ya hoping that the ?{1,50} would
specify
1-50 single characters of any type. but this finds blah and ya even if
they
are separated by many pages (hundreds of characters). what am i doing
wrong?

thanks,
kaston




--
kaston


  #3   Report Post  
kaston kaston is offline
Junior Member
 
Posts: 0
Default

Quote:
Originally Posted by Doug Robbins - Word MVP View Post
You could do it with a macro containing the following code:
whoa. thanks for the code. i've never used macros in word before but i'll try it out.

do you know why my reg exp search didn't work though? i can't see what i'm doing wrong
  #4   Report Post  
Posted to microsoft.public.word.docmanagement
Greg Maxey[_2_] Greg Maxey[_2_] is offline
external usenet poster
 
Posts: 668
Default search for strings separated by 1-n characters?

Kaston,

Weird yes.

Words Find feature is a little like water (runs in streams and takes the
path of least resistance).

Blah?{1,50}ya

Word finds Blah (its happy)
Word then finds 1 to 50 characters of anything (its happy)
Word then finds ya (its happy)

It doesnt care if there are 1 or 1,000,000 characters between that 50th
characer and ya. All it cares about is finding ya. If it exists it will be
happy to do so.

I don't know if there is a string that would meet your needs. Sorry.

kaston wrote:
Doug Robbins - Word MVP;442499 Wrote:
You could do it with a macro containing the following code:
whoa. thanks for the code. i've never used macros in word before
but i'll try it out.


do you know why my reg exp search didn't work though? i can't see
what i'm doing wrong


--
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.


Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I search for special characters Shiner Microsoft Word Help 2 May 1st 08 09:52 AM
how to search delete more than one characters in a document Rudy New Users 8 September 21st 07 07:13 AM
Search and Invisible Characters? Joseph McGuire Microsoft Word Help 3 March 4th 05 05:06 AM
search and replace symbol characters Patricia G. Kurz Microsoft Word Help 1 December 29th 04 03:11 AM
Make Word displays strings of text, not strings of code Xero Microsoft Word Help 2 December 9th 04 10:35 AM


All times are GMT +1. The time now is 07:04 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"