Substring search

H

Harry-Wishes

Hello

I am trying to execute a search for a particular string pattern that starts
with <p and and ends with >. (See example below). In particular, I want to
set up my find so that it is looking for a string pattern that contains the
substring "list" anywhere within the string.

<p class=MsoNormal
style='margin-left:.5in;text-indent:-.5in;mso-text-indent-alt:
-9.0pt;mso-list>


To give you an abbreviated version of what I am trying to do, below is a
snippet for the find method. You'll see that the substring has to appear
within a certain place within the complete string (not what I have in mind).
Any suggestions welcome. Thanks!

Harry_Wishes

With Selection.Find
.Text = "(\<p )(class=MsoNormal
style='margin-left:.)([0-9]{1,2})(in;text-indent:-.)([0-9]{1,2})in;mso-[!l]{1,30}list[!\>]{1,250}\>" '[!\>]{1,250}\>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
 
D

DaveLett

Hi Harry,
I think you're looking for something like the following:

Selection.HomeKey Unit:=wdStory

With Selection.Find
.Text = "\<p*^13"
.MatchWildcards = True
Do While .Execute
If InStr(1, LCase(Selection.Text), "list") <> 0 Then
MsgBox "This paragraph contains the following word: list"
End If
Selection.MoveRight
Loop
End With

HTH,
Dave
 
H

Harry-Wishes

Thanks Dave,

I see what your code does and in the majority of cases, this would be
sufficient but I was aiming for something more precise. In the beginning of
my program, I already have it so that it evaluates the presence of the
matching pattern in the Word document. I need a pattern matching expression
that has Word jump immediately to the string pattern upon execution of the
find method? There are reasons (too long to explain here) why I would like to
have this feature included in my program. The [!] would work if vba
supported the exclusion of a word and not just a single character.

Regards

Harry Wishes
 
D

DaveLett

Hi Harry,

If I understand you correctly, you want the routine to work so that the word
"list" is selected, correct? If so, then you can probably use something like
the following:

Dim oRng As Range
Selection.HomeKey unit:=wdStory

With Selection.Find
.Text = "\<p*^13"
.MatchWildcards = True
Do While .Execute
If InStr(1, LCase(Selection.Text), "list") <> 0 Then
Set oRng = Selection.Range
Do While InStr(1, oRng.Words.First.Text, "list") = 0
oRng.MoveStart unit:=wdWord, Count:=1
Loop
Do While InStr(1, oRng.Words.Last.Text, "list") = 0
oRng.MoveEnd unit:=wdWord, Count:=-1
Loop
oRng.Select
'''act on the word "list"
End If
Selection.MoveRight
Loop
End With

HTH,
Dave
 
H

Harry-Wishes

Hi Dave,

To clear up confusion, I'll give you an example of what I had in mind. Given
the string I pasted below (with the open and close angle brackets as part of
the string, I want the end result to be the entire string selected if and
only if the substring "list" is actually part of the string. The word "list"
could appear anywhere within the string except the very beginning or very
end. If I could write a find expression that, once executed, would find any
string matching the pattern below that must contain the substring in the
string irrespective of the substring's location, that would solve my issue.
So, if the substring "list" is not present, the find method simply won't
highlight anything. I hope that makes sense.

<p class=MsoNormal style='margin-left:mso-list-5in;-9.0pt;>


Regards,

Harry Wishes
 
D

DaveLett

Hi Harry,
Then I think you're looking for the following:

Selection.HomeKey unit:=wdStory

With Selection.Find
.Text = "\<p*\>"
.MatchWildcards = True
Do While .Execute
If InStr(1, LCase(Selection.Text), "list") <> 0 Then
MsgBox "Found."
End If
Selection.MoveRight
Loop
End With

You can't use
..Text = "\<p*list*\>"
as the search text because that would select the following (even though that
are different tags and different paragraphs in Word) with the .Execute method:
<p class=MsoNormal style='margin-left:mso- -5in;-9.0pt;>
<p class=MsoNormal style='margin-left:mso-list-5in;-9.0pt;>

Therefore, you end to just the tags first and then test if "list" is within
that string. If you want to search all tags (not just "<p "), then change the
..Text line to:
..Text = "\<*\>"

If I'm understanding correctly, you REALLY want the .Execute method with
wildcards to work this out so that you don't have do run the If Instr block,
correct? If that is correct, then I cannot find the pattern that would meet
your goals.

HTH,
Dave
 
H

Harry-Wishes

Thanks Dave,

I overlooked something fairly simple although you lost me a little when you
were explaining why .Text = "\<p*list*\>" was insufficient. Your search
expression works fine with the few variations of the string I've tested so
far. I'll continue testing in case I overlooked something else.

Thanks

Harry-Wishes
 
H

Harry-Wishes

Yes. I see what you were saying. The text = "\<p*list*\>" could potentially
include other adjacent tags with the word "list embedded". So this is just as
complicated as I had originally anticipated.

Harry Wishes
 
H

Harry-Wishes

Yes!

I was wondering what the "@" symbol does when I did my research. The
expression appears to begin the search with the open bracket, including
everything up to but not including the closing bracket. Hmmm, so the "@"
references the earlier part of the search expression by searching for the
presence of a specified string that is placed after the "@" symbol. Of
course, the full expression ends with the closing bracket to complete the
full search expression.

Thanks. Pretty niffty.

Harry Wishes

Tony Jollans said:
You can't use
.Text = "\<p*list*\>"
...

But can you not use this?

\<p[!>]@list*\>

--
Enjoy,
Tony

www.WordArticles.com

DaveLett said:
Hi Harry,
Then I think you're looking for the following:

Selection.HomeKey unit:=wdStory

With Selection.Find
.Text = "\<p*\>"
.MatchWildcards = True
Do While .Execute
If InStr(1, LCase(Selection.Text), "list") <> 0 Then
MsgBox "Found."
End If
Selection.MoveRight
Loop
End With

You can't use
.Text = "\<p*list*\>"
as the search text because that would select the following (even though
that
are different tags and different paragraphs in Word) with the .Execute
method:
<p class=MsoNormal style='margin-left:mso- -5in;-9.0pt;>
<p class=MsoNormal style='margin-left:mso-list-5in;-9.0pt;>

Therefore, you end to just the tags first and then test if "list" is
within
that string. If you want to search all tags (not just "<p "), then change
the
.Text line to:
.Text = "\<*\>"

If I'm understanding correctly, you REALLY want the .Execute method with
wildcards to work this out so that you don't have do run the If Instr
block,
correct? If that is correct, then I cannot find the pattern that would
meet
your goals.

HTH,
Dave

.
 
T

Tony Jollans

@ means one or more of the immediately previous expression. In this case the
previous expression is [!>] which means any character that is not a right
angle bracket.

--
Enjoy,
Tony

www.WordArticles.com

Harry-Wishes said:
Yes!

I was wondering what the "@" symbol does when I did my research. The
expression appears to begin the search with the open bracket, including
everything up to but not including the closing bracket. Hmmm, so the "@"
references the earlier part of the search expression by searching for the
presence of a specified string that is placed after the "@" symbol. Of
course, the full expression ends with the closing bracket to complete the
full search expression.

Thanks. Pretty niffty.

Harry Wishes

Tony Jollans said:
You can't use
.Text = "\<p*list*\>"
...

But can you not use this?

\<p[!>]@list*\>

--
Enjoy,
Tony

www.WordArticles.com

DaveLett said:
Hi Harry,
Then I think you're looking for the following:

Selection.HomeKey unit:=wdStory

With Selection.Find
.Text = "\<p*\>"
.MatchWildcards = True
Do While .Execute
If InStr(1, LCase(Selection.Text), "list") <> 0 Then
MsgBox "Found."
End If
Selection.MoveRight
Loop
End With

You can't use
.Text = "\<p*list*\>"
as the search text because that would select the following (even though
that
are different tags and different paragraphs in Word) with the .Execute
method:
<p class=MsoNormal style='margin-left:mso- -5in;-9.0pt;>
<p class=MsoNormal style='margin-left:mso-list-5in;-9.0pt;>

Therefore, you end to just the tags first and then test if "list" is
within
that string. If you want to search all tags (not just "<p "), then
change
the
.Text line to:
.Text = "\<*\>"

If I'm understanding correctly, you REALLY want the .Execute method
with
wildcards to work this out so that you don't have do run the If Instr
block,
correct? If that is correct, then I cannot find the pattern that would
meet
your goals.

HTH,
Dave

.
 

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