Word – Microsoft Office Word 2010: How to “Find and Replace” an entire line using “regular expressions” (wildcards)

microsoft wordmicrosoft-officemicrosoft-word-2010regexwildcards

I would like to "Find and Replace" entire lines in Microsoft Office Word 2010 which match some criteria.

I tried to find the solution in the official documentation, but I didn't find the answer:

(btw. here are two more useful unofficial documents: Find and Replace using wildcards; Regular Expressions in Microsoft Word)


Example

For example: I would like to search for all the lines which have the "Heading 2" style, and replace them so that they get between ## symbols: so this is the original text: This is my title, and I would like to find it and replace it this way: ## This is my title ## (this way I could transform a text's title into a Wiki-compatible heading 2 without any external plugins (EDIT: sorry, in the meantime I realized the MediaWiki-compatible heading 2 style uses == instead of ##, but it doesn't change the main point)).

So after Ctrl+H, checking "Use wildcards", selecting Format > Style > Heading 2, I've tried this and it did NOT work: (*^13>) or (<*^13>), where ^13 matches "Paragraph break / 'carriage' return" (see this) – this made Word search madly, and the GUI got frozen for minutes (!). At the end, it didn't find anything (but I DO have texts with Heading 2 style).

Microsoft Word 2010 - Find and Replace (*^13>) - Not Responding (Use Wildcards checked)

OK, this is not the appropriate pattern, so what would be the appropriate way to search and replace entire lines in Word?

Best Answer

Perhaps I'm missing something, but for some reason I wasn't able to do this in a single search/replace operation:

Steps

  1. Before:

    1

  2. Replace empty string (format Heading 2) with "## ^& ##" (no formatting):

    2

  3. After first replace:

    3

  4. Replace "^p ##" (no formatting) with " ##^p" (no formatting) (be aware that there is a space at the beginning of the replace string!):

    4

  5. After second replace:

    5

Related Question