0

I have created a VBA macro, which is able to find the below possibility of strings.

With mainSel.Find
    .ClearFormatting
    .MatchWildcards = True
    .Text = "[\{\[][a-zA-Z0-9\,\.\:\-;^l^13 ]@[\}\]]"
    .Wrap = wdFindContinue
End With
  • {xxxxx}
  • { xxxx }
  • {xxx }
  • {xxxx, xxxx}
  • { xxxx,xxxx}
  • {xxxx,xxxx }
  • { xxxx, xxxx }

But the current situation is, when it finds a field code in the document, it skips the all other occurrence of the matches. If I remove the space in the regular expression above it is processing the string after the fieldcode but it is skipping the strings which is having space in it like { xxxx}.

EX: this is an example {xxxx} -getting processed.this is the [fieldcode]. this is the string{xxx} which is not getting processed.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Rocky
  • 27
  • 8

1 Answers1

0

You need a while loop over occurences, since Execute returns True if the find operation is successful:

Option Explicit

Sub TestFind()
    Dim Content As Range
    Dim i As Long

    Set Content = ActiveDocument.Content
    ' let's print what we got so far
    Debug.Print "All text is:" & vbNewLine & Content.Text

    With Content.Find
        .ClearFormatting
        ' our find loop
        Debug.Print "Occurences: "
        While .Execute(MatchWildcards:=True, Wrap:=wdFindStop, FindText:="[\{\[][a-zA-Z0-9\,\.\:\-;^l^13 ]@[\}\]]")
            ' count occurences
            i = i + 1
            ' lets bold text just for testing purposes
            Content.Bold = True
            ' and also print each occurence
            Debug.Print vbTab & "Occurence #" & i & vbTab & Content.Text
        Wend
    End With
End Sub

Output:

All text is:
{xxxxx} 
asdasd
{ xxxx } 
As{asd}
asff
{xxx } 
{xxxx, xxxx} 
safasf
{ xxxx,xxxx} 
{xxxx,xxxx } 
{ xxxx, xxxx }


Occurences: 
    Occurence #1    {xxxxx}
    Occurence #2    { xxxx }
    Occurence #3    {asd}
    Occurence #4    {xxx }
    Occurence #5    {xxxx, xxxx}
    Occurence #6    { xxxx,xxxx}
    Occurence #7    {xxxx,xxxx }
    Occurence #8    { xxxx, xxxx }

Also, this isn't a regex at all and thoose patterns are really crude. If you need a real regex, take a look here.

CommonSense
  • 4,232
  • 2
  • 14
  • 38
  • Hi, Thanks for you help... but in the text we have a field code between the required strings... once it find the field code it is not reading the next occurence. For Example: All text is: {xxxxx} [field code] asdasd { xxxx } As{asd} asff {xxx } {xxxx, xxxx} safasf { xxxx,xxxx} {xxxx,xxxx } { xxxx, xxxx } In this situation it only reads the first occurence. i have tried the provided solution and it gives the below output: Occurences: Occurence #1 {xxxxx} Occurence #2 Occurence #3 . . . . infinite – Rocky Jun 14 '17 at 09:26
  • @Rocky, it's works for me with your "all text". I [got 9 occurences](https://i.stack.imgur.com/2XyIO.png) (your field code catched too), so I assume that it's what you desire, isn't it? Unfortunately, I don't familiar with those patterns. – CommonSense Jun 15 '17 at 09:23
  • @Rocky, also, I suggest you to drop those ugly patterns too and use a RegEx instead! There're a lot of sites like regex101.com where you can paste a lot more of data and test patterns on-line. I'm sure, pattern for rule "grab all inside curly braces with them and if there're square braces after that - grab them too with same fashion" much more easier and more readable, if I understand your rule right. So try it with real data and if you stuck again - update your question with link to regex101. Cheers! – CommonSense Jun 15 '17 at 12:12
  • Thanks for you help.. now i am using the below regex . but i need to loop through each and ever matches and replace the string if needed.. could you please help.. `Sub sampleregex() Dim regEx As New RegExp regEx.Pattern = "[\{\[][A-z0-9\s\.\,\:\;]*[\]\}]" regEx.Global = True Dim match As Object Dim Content As Range Set Content = ActiveDocument.Content Dim matches As MatchCollection Set matches = regEx.Execute(ActiveDocument.Range) For Each match In matches Debug.Print vbTab & "Occurence #" & vbTab & match [matched string = "newstring"] Next match End Sub` – Rocky Jun 15 '17 at 16:14