Skip to main content
Inspiring
July 20, 2018
Answered

GREP query to change space to tab

  • July 20, 2018
  • 1 reply
  • 2185 views

There are many paragraphs and at the beginning of each paragraph, there are one or two characters (they could be letters or numbers or a period) followed by a space, an = sign and a space, and then any number of more characters.

I need to change the space to tabs.

For example the original text has a space character before and after the =

AB = Blah blah blah

needs to change to this where there is a tab character before and after the =

AB     =     Blah blah blah

Anyone know a GREP find/change query for this?

This topic has been closed for replies.
Correct answer Jongware

For 1:

^[\w.]+\K *= *

replace with \t=\t

This looks for any sequence of letters or numbers (\w, "word" characters, which include uppercase and lowercase as well as digits, and the underscore for historical reasons) or a period at the start of a paragraph, then discards what it found. It then matches zero or more spaces, the equals, and optionally more spaces. This latter part gets replaced with a tab, equals, tab.

For 2:

^[\u\l]\.\K +

replace with a single tab. This looks for 'any letter' (one of uppercase or lowercase), followed by a period. Then it discards this, matches any positive number of spaces, and these get replaced with your tab again.

I used the fairly new addition "\K" in both expressions because the alternative is to construct elaborate lookbehind expressions -- quite basic for #2, but barely doable for #1 (it is do-able but this is so much easier).

1 reply

Inspiring
July 20, 2018

I also need a GREP query to change multiple spaces to a single tab character.

For example: with multiple spaces after the A period:

A.   Blah blah blah

need to change to a single tab instead of multiple spaces after the A.

A.     Blah blah blah

The letter would not necessarily be an 'A', it could be any letter.

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
July 20, 2018

For 1:

^[\w.]+\K *= *

replace with \t=\t

This looks for any sequence of letters or numbers (\w, "word" characters, which include uppercase and lowercase as well as digits, and the underscore for historical reasons) or a period at the start of a paragraph, then discards what it found. It then matches zero or more spaces, the equals, and optionally more spaces. This latter part gets replaced with a tab, equals, tab.

For 2:

^[\u\l]\.\K +

replace with a single tab. This looks for 'any letter' (one of uppercase or lowercase), followed by a period. Then it discards this, matches any positive number of spaces, and these get replaced with your tab again.

I used the fairly new addition "\K" in both expressions because the alternative is to construct elaborate lookbehind expressions -- quite basic for #2, but barely doable for #1 (it is do-able but this is so much easier).

Inspiring
July 20, 2018

Thank you for this, Jongware -- you are truly the best!