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

How to write with grep: delete the space except the first one in the paragraph?

Enthusiast ,
Jun 20, 2022 Jun 20, 2022

Copy link to clipboard

Copied

There is a paragraph with many spaces inside. I need to keep the first space in each paragraph and replace all the spaces behind with none. How grep writes.

 

Snipaste.png

------------------------------------------------

Sample:
土壤 分为3类:亚高山草甸土、砖红壤性红壤、水稻土。适宜 种植的农作物有水稻、小麦、包谷、豆类和 薯类;经济作物主要 适宜 种植茶树、油菜、烤烟等。

------------------------------------------------

 

Thanks

 

TOPICS
Feature request , How to , Performance , Scripting

Views

841

Translate

Translate

Report

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

Community Expert , Jun 22, 2022 Jun 22, 2022

Uwe, I think you've misinterpreted the request. OP has aske to retain first space in paragraphn o mention of colons (and there is none preceding the first space in illustration above).

Might not be possible to do the nested style, depending on the complexity of the other text styling, but worth a look. My GREP is kind of rusty so I'm having trouble isolating the first space to use Find/Change if a nested style won't work, but maybe you have a way.

Votes

Translate

Translate
Community Expert , Jun 22, 2022 Jun 22, 2022

Your ^.+? \K.+$ does work in 2022, Uwe. So you can apply a character style to everything after the first space (safest to do this as a GREP style), but if you don't want to use a GREP style it's safest to use a text condition. But you need to define the  style or the condition, apply it, do whatever you want with the captured spaces, then remove the style or the condition.

 

Anyway, as Brian suggested, a script would be easiest, something like this:

 

par = app.selection[0].parentStory.paragraph
...

Votes

Translate

Translate
Community Expert ,
Jun 20, 2022 Jun 20, 2022

Copy link to clipboard

Copied

Don't think can be done with GREP alone. You'd need a script. 

Votes

Translate

Translate

Report

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
Enthusiast ,
Jun 21, 2022 Jun 21, 2022

Copy link to clipboard

Copied

Why, it doesn't work:

 

(?<!(^~K+))(\s)

 

I seem to know that (? < (^~k+)) will not work.

 

But=~ K is sometimes feasible

 

 

 

That is, reverse query can only specify one character, while forward query can specify multiple characters.

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

~k is a discretionary line break. What does ~K stand for?

Votes

Translate

Translate

Report

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
Enthusiast ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Sorry, I made a mistake.

It should be ~K, not ~k, referring to any Chinese character.

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Hi dublove,

is it possible to apply a new character style to the text after the full width colon?

( At least I assume your screenshot is showing a full width colon with Unicode value FF1A. )

If yes this could be a two-step process with GREP Find/Change.

 

First find all text after the full width colon to the end of the paragraph:

^.+\x{FF1A}\K.+$

Apply a new unique character style which is just a helper format. No particular text formatting.

 

Then do a second GREP Find/Change.

Find text formatted with that new unique character style. And all white spaces with that style.

\s

Replace with nothing.

 

Remove the new character style with character style [None].

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Think I'd go the opposite way -- apply the character style to the first space, then replace all spaces with character style None with nothing. I'm a bit leery of applying a character style to so much text, and a style applied to only one space seems more innocuous. That could even be set up as a nested style in your paragraph style so you wouldn't need to run Find/Change to apply it.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Yes, I did later.

The first space is easy to find.

I will replace the first blank with @k

Then, delete all spaces, and finally replace @k with full width spaces

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Hi Peter,

I have no idea how the rest of the text is structured.

If paragraph styles are used; or not.

So let's see what dublove has to say.

 

My assumption in my last post was of course that only one full width colon is used per paragraph.

I could be wrong. So I would change my first GREP Find pattern to:

^.+?\x{FF1A}\K.+$

 

Or we could also look for the first white space in a paragraph and format all following text in the same paragraph.

But that pattern could be too general, perhaps. OP dublove should know…

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Uwe, I think you've misinterpreted the request. OP has aske to retain first space in paragraphn o mention of colons (and there is none preceding the first space in illustration above).

Might not be possible to do the nested style, depending on the complexity of the other text styling, but worth a look. My GREP is kind of rusty so I'm having trouble isolating the first space to use Find/Change if a nested style won't work, but maybe you have a way.

Votes

Translate

Translate

Report

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
Enthusiast ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

There is no: before the paragraph

样本: it means sample. (my paragraph does not include the previous one. This one is a hint. The next one is a sample.)

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Hi Peter,

well, all text in a paragraph after the first white space can be found this way:

^.+?\s\K.+$

If you want to find the first space only, one could do this like that:

^.+?\K\s

 

But there are two phenomenons with both find patterns that I cannot explain.

Below the results in yellow that I can see in various situations for my second pattern:

GREP-FindPattern-MarkFirstWhitespaceInParagraph.PNG

[1] It seems that an "end of line" special character can interfere the result.

[2] Initially I thought that .+? stands for one instance or more with shortest result for a character.

But here it seems, that it stands for two instances or more with shortest result.

 

Important: Both obstacles also can be found in my first pattern.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

It's nothing to do with the forced paragraph break, Uwe. Because of \K InDesign finds every other instance, not every instance. I think that that's a bug.

P.

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Aha!

Ok. The other thing with .+? could be perhaps seen in conjunction with the new behavior that a simple ^ could find an insertion point at the beginning of a paragraph. Or after the first line break special character in a paragraph. ( InDesign 2021 and above. )

My pattern

^.+?\s\K.+$

will not work at all with InDesign 2020. Just tested this.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Votes

Translate

Translate

Report

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 ,
Jun 22, 2022 Jun 22, 2022

Copy link to clipboard

Copied

Your ^.+? \K.+$ does work in 2022, Uwe. So you can apply a character style to everything after the first space (safest to do this as a GREP style), but if you don't want to use a GREP style it's safest to use a text condition. But you need to define the  style or the condition, apply it, do whatever you want with the captured spaces, then remove the style or the condition.

 

Anyway, as Brian suggested, a script would be easiest, something like this:

 

par = app.selection[0].parentStory.paragraphs.everyItem().findGrep();

for (i = 0; i < par.length; i++) {
  for (j = 1; j < par[i].length; j++) {
    par[i][j].contents = '';
  }
}

 

 In other words, get all spaces per paragraph, then delete them all except the first one in each paragraph.

Votes

Translate

Translate

Report

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
New Here ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

grep      (^~K+ )(.*)( )(.*)( )(.*)( )(.*)( )

replace   $1$2$4$6$8

111.png

Votes

Translate

Translate

Report

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
New Here ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

or you can try the following

222.png

Votes

Translate

Translate

Report

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
Enthusiast ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

It is not fixed. There are two Chinese characters in front of it. The number of spaces is not fixed.

Votes

Translate

Translate

Report

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 ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

@dublove -- I modified the code I posted so that it deletes spaces after the first space (the underlines in my original example were just for illustration).

Votes

Translate

Translate

Report

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
New Here ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

can you reissue the code you modified, thank you

Votes

Translate

Translate

Report

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 ,
Jun 24, 2022 Jun 24, 2022

Copy link to clipboard

Copied

LATEST

Select the text frame that holds your text or click anywhere in the text, then run the script:

app.findGrepPreferences = null;
app.findGrepPreferences.findWhat = '^.+?\\h\\K.+';
par = app.selection[0].parentStory.paragraphs.everyItem().findGrep();

for (i = 0; i < par.length; i++) {
  for (j = 1; j < par[i].length; j++) {
    par[i][j].contents = '';
  }
}

 

Votes

Translate

Translate

Report

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
New Here ,
Jun 23, 2022 Jun 23, 2022

Copy link to clipboard

Copied

setp1   ~(

remove all spaces(there may be other types of spaces in your document, please check)

 

setp2  (^~K+)(分为)      →      $1~($2

because "分为" is fixed, so it can be set as a search condition

 

it can be changed regardless of the length of the noun at the beginning of the paragraph
i've tried and succeeded

 

Votes

Translate

Translate

Report

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