Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Need Grep help

Explorer ,
Sep 15, 2021 Sep 15, 2021

I want to remove the space before some punctuation like sapce before period, space before coma, spce before question mark etc.. 

 

Can I remove the spaces with single GREP or indesign script. 

 

 

Help me to find the right solution

 

Thanks in advance

 

TOPICS
Print , Scripting
692
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Advisor , Sep 15, 2021 Sep 15, 2021

Hello @Nousheeth,

 

You can do it with a single GREP like this...

Search for \s+([!,\.:;?]) and replace it with $1 (in the search, include whatever punctuation symbols you want inside the brackets, but remember to 'escape' reserved characters with a back slash \ like what's done with the period).

Screen Shot 2021-09-15 at 9.56.14 PM.png

Regards,

Mike

Translate
Community Expert , Sep 15, 2021 Sep 15, 2021

I would use a class like [,.?!;:]  with the punctuation you want to find as a look-ahead. You want to be careful, though, because there are legitimate places where space before punctuation is expected, quotation marks, parentheses and brackets for example, so the general class [[:punct:]] would not be a good idea, and some sorts of whitespace may also be legit, such as tabs and paragrap returns.

From Peter Kahrel's book I find that \p{space_separator} finds any white space other than tab or retu

...
Translate
Advisor ,
Sep 15, 2021 Sep 15, 2021

Hello @Nousheeth,

 

You can do it with a single GREP like this...

Search for \s+([!,\.:;?]) and replace it with $1 (in the search, include whatever punctuation symbols you want inside the brackets, but remember to 'escape' reserved characters with a back slash \ like what's done with the period).

Screen Shot 2021-09-15 at 9.56.14 PM.png

Regards,

Mike

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2021 Sep 15, 2021

I don't think you need to escape the . inside a class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2021 Sep 16, 2021

I don't think you need to escape the . inside a class.

 

Correct.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 16, 2021 Sep 16, 2021
LATEST

Thanks Mike

 

Its Working 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2021 Sep 15, 2021

I would use a class like [,.?!;:]  with the punctuation you want to find as a look-ahead. You want to be careful, though, because there are legitimate places where space before punctuation is expected, quotation marks, parentheses and brackets for example, so the general class [[:punct:]] would not be a good idea, and some sorts of whitespace may also be legit, such as tabs and paragrap returns.

From Peter Kahrel's book I find that \p{space_separator} finds any white space other than tab or return {not sure if that covers both hard and soft returns, both of which I think you would want not to be included), so you might try starting with something like \p{space_separator}+(?=[,.?!;:])

I have not tested this...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2021 Sep 15, 2021

Forgot you would want to replace with nothing.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2021 Sep 16, 2021

\p{space_separator} finds any white space other than tab or return

 

Correct.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2021 Sep 16, 2021

Does it omit both forced line breaks and paragraph breaks?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines