Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have several Word documents where line breaks (paragraph breaks) have been added purely for cosmetic reasons (probably by a human, but maybe by an OCR system or something similar). I want to remove these extra line breaks from the documents. Basically, an 'extra' line break is one that is surrounded by lower-case letters on either side (with optional whitespace). Unfortunately, though, if I want to find paragraph breaks in Word (^p) I can't use character classes to find only lower-case letters ([a-z]) and vice versa.

Basically I want to use a multiline regex on the document so I can find something like the following:

/[a-z]\s*\n\s*[a-z]/

and replace the newline with a space. Is there any way I can search for both paragraph marks (^p in Word) and character classes (or just lower-case letters in general)?

EXAMPLE:

TITLE¶
This is some text.

would not match, but

this text is on one line and¶
goes on to the next line.

would match and the “¶” would be replaced by a space.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I can’t tell from what you’ve said whether you know that, if you click on More >> in the Microsoft Word “Find and Replace” dialog box, you get a “Search Options” panel that includes a “Use wildcards” option.  Note that it supports an arcane wildcard language, not regular expression notation.  To begin with this option, use [a-z]^13[a-z].  For some reason, you can’t use ^p in a wildcard search, but ^13 is the wildcard-enabled equivalent of ^p.

The whitespace is a little trickier.  The best I can come up with is that you have to do the search four times, using

  • [a-z]^13[a-z]
  • [a-z][^t ]{1,99}^13[a-z]
  • [a-z]^13[^t ]{1,99}[a-z] ,   and
  • [a-z][^t ]{1,99}^13[^t ]{1,99}[a-z]

since, oddly enough, ^t works in wildcard mode.  \s and * don’t mean what they mean in regular expressions.  {n,m} does work, but n has to be positive.  And note that you can’t just replace matches with a space, since the last preceding letter and the first following letter are included in the match, and would get clobbered.

For extra credit: You might want to look for a - (hyphen) as the last printing character before the line break; but be sure to address these two (different) cases:

                                                          … surrounded by lower-¶
case letters on either side (with optional whitespace).  Unfor-¶
tunately, though, …

share|improve this answer
    
^13 was exactly what I was looking for. Thanks! So strange that it makes you change it when turning on wildcards. It would be nice if Word supported full regex, but this wildcard language will work for now. PS - do you have a link to a list of all those numbered wildcards? I couldn't easily find one. –  Drewmate Jun 28 '13 at 15:19
    
office.microsoft.com/client/… –  Scott Jun 28 '13 at 15:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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