22

Is there a way to look up patterns inside an MS Word document instead of an exact match?

I have a document where all decimals have been written as / instead of .. For example, 1.02 has been written as 1/02, because Persian typography uses / instead of . for the decimal point.

As / is not only used for decimals, and it has other applications, for example, fractions, percentage, etc., is there a way to find a pattern like N/M within a word document, where N denotes a decimal and M also shows another decimal number?

  • 2
    How is this not a duplicate more than 5 years after Super User launched? – Peter Mortensen Nov 30 '14 at 12:10
  • 1
    A starting point may be Regular Expression for Find and Replace in Microsoft Word. – Peter Mortensen Nov 30 '14 at 12:19
  • Well it would be hard to move it to notepad if there were other text in the middle, lets assume that one is writing a journal article(please use latex instead) so they cant move it out of word, or something of that nature, they would loose all the other formatting. – Charm_quark Dec 2 '14 at 8:08
  • 1
    I've just verified: the latest MS Word versions store Word documents (*.docx) as zipped XML based documents. Once you know how this structure works, you might unzip your *.docx file and do a pattern match in the resulting XML files structures. – Dominique Jun 6 '17 at 11:15
  • @Dominique that is interesting. Didn't know that – codezombie Jun 6 '17 at 11:36
10

like 'N/M' within a word document, where N denotes a decimal and M also shows another decimal number

Just for a search you can use ^#/^# as your search parameter. Under the more options, and find Special, there are some other options that can be helpfull.

Nb: ^# , is one decimal place so if you are looking specifically for x/yy then it would be ^#/^#^# etc.

to Replace,

This is a better option , perform search with ([0-9])/([0-9]) and replace with \1.\2 , and enable "use wildcards"

  • Thanks. this finds the hits. but Word does not accept ^#.^# for the replacement part. I want to leave the digits surrounding '/' intact and replace '/' with '.'. – codezombie Nov 30 '14 at 8:08
  • 1
    try doing a search with this, "([0-9])/([0-9])" and replace with "\1.\2" , take out the double quote. and enable "use wildcards" – Charm_quark Nov 30 '14 at 8:19
27

Word

Use Microsoft's implementation of regular expressions

Press CTRL+H » click on More » Enable Use wildcards

  • Find what: ([0-9]{1,2})/([0-9]{2})
  • Replace with: \1.\2

enter image description here enter image description here

This looks like a crazy search pattern so let's examine it:

  • [0-9] stands for a single numeric value (0,1,2,3,4,5,6,7,8 and 9)
  • {1,2} is used for counting occurrences of the previous character or expression. In our case this means: Search only for one or two numerics.

    Caution: If you're using a german Word, you have to use ; as separator instead of ,

  • / has no special meaning. It literally searches a slash
  • () the round brackets are important. They divide the pattern into logical sequences so we can later use \1,\2...\n in our replace pattern. This way we preserve values

Read more on the section The expressions, Piece by piece in Microsoft's support article or on Graham Mayor's Word site


VBA (better solution)

I suggest you to use a VBA method which adheres more to the default from other RegEx engines. They are better documented and more people can help you.

This macro asks for a RegEx pattern to search in the whole document and replace it with your given string.

Press ALT+F11 to open the VBA editor. Paste the code anywhere and execute it with F5

Sub RegexReplace()

    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")    
    On Error Resume Next

    RegEx.Global = True
    RegEx.Pattern = InputBox("Find what:")
    ActiveDocument.Range = _ 
        RegEx.Replace(ActiveDocument.Range, InputBox("Replace with:"))        

End Sub
  • Find what: (?!\d)/(?=\d)
  • Replace with: .

enter image description hereenter image description here

  • 10
    A good answer takes time. Too much time – nixda Nov 30 '14 at 9:17
  • I can't believe Microsoft called regular expression wildcards. smh – Nik-Lz Sep 19 '17 at 12:58

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.