Solved

Regex in Word VBA

Posted on 2014-01-10
13
1,018 Views
Last Modified: 2014-01-20
Hello,
     This is related to this question: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_28334735.html

What I am trying to do is find text that matches this regex:
(ARTICLE)(\s+)(\d+)(\s+)(–)(\s+)(.*)(\n)

Open in new window

(The string ARTICLE followed by a space, followed by a digit, followed by a space, followed by an em dash, followed by a space, followed by any number of words, then a newline. An example is "ARTICLE 5 – DESCRIPTION...")

And this regex:
(\d+)(\.)(\d+)(?:\.)(?:\d+)(\s+)(.*)(\n)

Open in new window

(An integer, followed by a period, followed by another integer with an optional period and integer, followed by any text, and a newline. An example is "2.1.3 Subsection 2...")

If text matches, the macro should apply a style that includes heading level so I can generate a Table of Contents and populate the navigation pane. If my regex is incorrect, please correct me.

Thank you!
0
Comment
Question by:indigo6
  • 9
  • 3
13 Comments
 
LVL 14

Expert Comment

by:DrTribos
ID: 39772860
To give you an idea of the syntax, this will find up to Description...
(ARTICLE )[0-9]( )^=( )<[0-9A-z ]*>

Need to turn Wild Cards on in the search
0
 
LVL 14

Accepted Solution

by:
DrTribos earned 500 total points
ID: 39772996
And this:

(ARTICLE )[0-9]( )^+( )<[0-9A-z ]*[^13]

will find the first paragraph (including marker) starting with "Article #"

note:
em dash is ^+
en dash is ^=
0
 
LVL 14

Assisted Solution

by:DrTribos
DrTribos earned 500 total points
ID: 39773010
And this should do the second part...
[0-9]{1,}[.][0-9]{1,}[*][! ]( )<[0-9A-z ]*[^13]
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39773064
You mention VBA, so I assume that you are implementing the VBScript regex objects.  In that case, your pattern string for the 2nd example should be:

(\d+)(\.)(\d+)(\.)?(\d+)?(\s+)(.*)(\n)

Not that DrTribos's recommendation is for using the search function built into Word, which is accessible via VBA.
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39773067
Ahhhh... I was wondering.  I've never used RegEx.
0
 

Author Comment

by:indigo6
ID: 39777322
DrTribos, actually that's fine, I was wondering about what the wildcard syntax would be! I tweaked the second one to be (ARTICLE )[0-9]( )^=( )<[0-9A-z ]*[^13] since it was an En dash, and I had to run a second round with (ARTICLE )[0-9][0-9]( )^=( )<[0-9A-z ]*[^13] to detect article numbers 10 and larger. But that worked great!

     The second one has me perplexed though. It detected strings like the following:
2.2 Access
2.7 Employee Time Off
21.11.3 Class Size (Unit 3)

But not:

5.2 No Lockout
5.4 Savings Clause

From what I can tell, they are formatted the same way. I'm not sure why it does not find these.

matthewspatrick, thanks for the VBA regex! How would I write a macro that changes the style of strings that match that regex?

Thanks!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:DrTribos
ID: 39778446
Sorry I thought I posted this yesterday - apparently it failed.  Here is some code to make the changes using the wild card...
Sub UpdateSections()

Dim para As Paragraph
Dim str As String

str = "(ARTICLE )[0-9]( )^=( )<[0-9A-z ]*[^13]"

ActiveDocument.Range.Select
    With Selection.Find
        .Text = str
        .Replacement.Text = ""
        .Forward = True
        .Font.Name = "Arial Narrow"
        .Font.Size = 24
        .MatchWildcards = True
        .Forward = True
        .Replacement.Style = "Heading 1"
        .Execute Replace:=wdReplaceAll
    End With

End Sub

Open in new window


You'll have to change the font names and sizes to suit... :-)
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39778450
Gah! My other comment is also missing - regarding the strange results... perhaps there is a double space or something confusing the issue.  Can you copy a few of the offending lines and paste them into a sample document?

Also, not sure if you are in the habit of using show - hide... the backward P thing on the home tab of the ribbon (paragraph marker in the paragraph section)... this will show spaces and might make diagnosis easier :-)
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39778456
Another possible reason that the text is not detected is because there is a soft return instead of a hard return... soft return looks like the symbol on the keyboard for the enter button. Hard return is the 'backward P' paragraph marker....  The wild card search is explicitly looking for a hard return (^13), not sure how other characters would impact...
0
 

Author Comment

by:indigo6
ID: 39783981
They seem to all be hard returns. Any other reasons why this could be?
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39784155
Pls copy the offending paragraphs to a sample document and upload.
Sorry for the brevity - on phone
0
 

Author Closing Comment

by:indigo6
ID: 39795552
I would have to strip too much of the info out, plus the requirements changed, so I'll close this question. It got me moving in the right direction though. Thanks!
0
 
LVL 14

Expert Comment

by:DrTribos
ID: 39796168
No worries - glad to help :-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now