Processing
 

VBA regex matching over two lines

20/09/2016 16:31#1

Andreas K

Member

Joined at: 6 months ago

Post: 2

Thank: 0

Thanked: 0

I created a VBA macro, which searches regular expressions in txt documents. You have to input a regular eypression in a cell in an excel spreadsheet and the macro will use Word to count the occurences of the regex in a particular txt document. So far everything works great.

Now I am trying to search for phrases like "going to". Which also works fine, as long as it is not spread out over two lines. Potentially, there is a line break between "going" and "to". So I tried patterns like:

going\nto going[\n]to going\rto going\sto 

None of them found "going to" spread out over two lines. I also entered it manually in the txt documents and couldn't find it either.

When I only search for \n. The line breaks between "going" and "to" are found correctly.

What am I doing wrong?

20/09/2016 17:49Top#2

Joined at: 12 months ago

Post: 15

Thank: 0

Thanked: 0

The best way to match whitespace between two words is \s+.

It matches 1 or more whitespace symbols.

Here is a sample VBA piece of code printing 3 "going on"s that are separated with 1) just a CR, 2) CR+LF, 3) spaces.

Sub MatchStrWithWhitespaces() Dim rx As RegExp Dim m As MatchCollection Dim s As String  s = "going" & vbCr + "to and " & "going" & vbCrLf + "to and " & "going    to and " Set rx = New RegExp rx.Global = True rx.Pattern = "going\s+to" Set m = rx.Execute(s) If m.Count > 0 Then   For Each n In m     Debug.Print m(0)   Next End If  End Sub 
Similar articles

VBA regex matching over two lines

6 months ago - Reply: 1 - Views: 44

Table output to Word-2011 from Rstudio using knit

6 months ago - Reply: 0 - Views: 48

Write a Collection of paragraphs into a new document

6 months ago - Reply: 1 - Views: 72

how to write paragraphs without show to document

6 months ago - Reply: 1 - Views: 67

numericals in word macro

6 months ago - Reply: 1 - Views: 46

Can not find proofing tools

6 months ago - Reply: 0 - Views: 3

Possible values for <w:outlineLvl> in Word OOXML

6 months ago - Reply: 1 - Views: 8

C# restrict editing partial word paragraph

7 months ago - Reply: 2 - Views: 146

Macro translation Please (possible virus)

6 months ago - Reply: 1 - Views: 35

Doc/Docx Fastest Reading Method for Searching

6 months ago - Reply: 0 - Views: 3

Save embedded file / content from word

6 months ago - Reply: 0 - Views: 6

Counting words in Word document, including footnores

6 months ago - Reply: 1 - Views: 24

c# access extract word ole object

6 months ago - Reply: 1 - Views: 25

Using word wildcards to find unaccepted changes

6 months ago - Reply: 1 - Views: 46

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re