What Regular Expression Can I Use To Search And Replace The Following in MS Word?

2

I have lots of text in Microsoft Word similar to the following:

1 The quick brown 2 fox jumps over 3 the lazy dog.

I need some help to find a regular expression that finds the numbers and inserts line breaks before them, resulting in the following format:

1 The quick brown
2 fox jumps over
3 the lazy dog.

I would greatly appreciate anyone's help!

John Michael Wilkinson

Posted 2019-06-08T19:10:40.597

Reputation: 33

1Your before and after texts are identical ... – DavidPostill – 2019-06-08T19:23:17.987

You need to format your text correctly. I suggest formatting as code. – AFH – 2019-06-08T19:33:39.913

Yeah, sorry I did format the text correctly, but when I add it to the comment box it is okay. When I hit OK, the formatting is not retained? Don't know how to format as code?? The formatting I need is to have all the numbers at the left side of the the text, i.e. a line break before each number. I have been searching for ways to achieve this, but nothing so far! – John Michael Wilkinson – 2019-06-08T20:15:06.137

Ok, I finally managed to format the text correctly to show what I'm asking! I think my question is ready for answering... – John Michael Wilkinson – 2019-06-08T23:20:01.297

Answers

2

Go into “Find and Replace”, click on “More > >”, and click “Use wildcards”.

(But you already knew that, right?)

Set “Find what” to {1,9}([0123456789])
Note that the first character is a space, and the second character is {.

Set “Replace with” to ^p\1
(Caret, lower-case “P”, backslash, “one”)

  • The {1,9} matches a string of between one and nine spaces.  This allows multiple spaces in the input; e.g.,

    1 The quick brown      2 fox jumps over 4 the lazy dog.

  • [0123456789], of course, matches a number (specifically, a digit).  You can probably use [0-9] instead.
  • The () delimit a capture group.  We want to capture the digit and ignore the spaces.
  • ^p, of course, is a paragraph break (the equivalent of typing Enter).  If you want a line break (the equivalent of typing Shift+Enter), use ^l (lower case “L”).
  • \1 substitutes the first capture group.

So: find a number, and replace it and the preceding spaces with itself preceded by a break.  In other words, replace spaces with break before a number.

G-Man Says 'Reinstate Monica'

Posted 2019-06-08T19:10:40.597

Reputation: 6 509

Hey G-Man, thanks for your help on that! The line break seems to work best for my needs! Your expression works perfectly and is a big help to me! Much appreciated! Also, seems like you formatted my example above, so thanks for that as well! All the best! – John Michael Wilkinson – 2019-06-26T15:29:18.303

Yes, your answer did solve my problem! Works great! Thanks once again! I accepted your answer as the solution to my question! 5***** – John Michael Wilkinson – 2019-06-27T17:28:23.930