Regular expression to find the beginning of a paragraph in Word

3.2k views Asked by At

I'm using Word 2016 and I need to delete some paragraphs, beginning or ending with known words. For example, in the following text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.¶

Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua,¶

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea.¶

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut enim ad minim veniam, ...¶

Paragraphs starting with a given word/string work as expected (delete 1st and 4th paragraphs):

Find What: "Lorem ipsum dolor *^13" | Replace With: "" | Wildcard: On

Paragraphs ending with a given word/string are not working (delete 2nd paragraph). Tried:

  • "^13* magna aliqua,^13" ^10 = CR - deletes everything on the left, including previous paragraphs
  • "^10*magna aliqua,^13" ^10 = LF - no changes

I've also tried some interesting examples on this site http://word.mvps.org/.../usingwildcards.htm but didn't find the solution.

Is there any way of doing this without VBA?

2

There are 2 answers

0
Armali On

"^13* magna aliqua,^13" ^10 = CR - deletes everything on the left, including previous paragraphs

That's understandable, because the * matches any characters, including the CR, so the matched part extends from the first found CR over any paragraphs lain inbetween up to the one ending with magna aliqua,.

In order to not match other paragraph endings, we have to use something other than *:

[!^13]@ magna aliqua,^13

The [!^13] means "any character except CR", the @ means "one or more occurrences of this".

0
macropod On

To delete all paragraphs beginning with 'Lorem', as per your example, you might use a wildcard Find/Replace, where:

Find = ^13Lorem[!^13]{1,}
Replace = nothing

To delete all paragraphs ending with 'magna aliqua,', as per your example, you might use a wildcard Find/Replace, where:

Find = [!^13]@magna aliqua,^13
Replace = nothing

You can't really combine these into a single Find/Replace that with find either - only one that will find both.