18 Replies Latest reply on Sep 1, 2013 12:19 PM by JoaoCP

    replace space with non breaking space - grep

    lister110 Level 1

      Hi everyone,

       

      Sorry, I have another grep question. I need to replace a space with a non breaking space ie 20<normal space>mg with 20<non breaking space>mg. I can select any digit and space, but when I add a non breaking space but it doesnt replace the normal space?

       

      Thanks..

       

       

      lister

        • 1. Re: replace space with non breaking space - grep
          Sajeev Sridharan Level 4

          Use this

           

          Find: (\d+)( )(mg)

          replace: $1~S$3

           

          Vandy

          • 2. Re: replace space with non breaking space - grep
            vijayanpvl Level 1

            Use following grep, it finds only number and space after number then replace space with non-breaking space

             

            find: (\d+)space

            replace: $1~S

             

            Untitled-1.JPG

            • 3. Re: replace space with non breaking space - grep
              Peter Spier Most Valuable Participant (Moderator)

              I would use (\d)<press spacebar>(\w) as a more generic case. You don't need to find more than one didgit befor the space, if it's a multidigit number, since you aren't replacing anyting in the number, but you do want to be sure that the space is followed by some word character, not punctuation or a break of some sort.

               

              Replace with either $1~S$2 or $1~s$2 depending on if you want flexible or fixed-width, repectively, non-breaking space.

              • 5. Re: replace space with non breaking space - grep
                ArchaeoJoFo Level 1

                Hi Peter,

                 

                I have kind of similar problem. I need to replace space with non breaking space in a more complexe way.

                Example:

                • 10. Jh. v. Chr. or
                • 7. Jh. n. Chr. or
                • 3200 v. Chr. or
                • 400-200 v. Chr.

                every space has to be replaced as a cleanup for text coming from different authors. I'm completely new in GREP and started with this search:

                \d{1,2}\.\s[a-zA-Z]{2}\.\s[n|v]\.\s[a-zA-Z]{3}\.

                to find some of the examples above but that didn't take me any further.

                 

                Thanks Chris

                • 6. Re: replace space with non breaking space - grep
                  jmlevy Adobe Community Professional

                  I am not sure I unterstand well because English is not my native language, but I wonder why you don't run a standard search and replace if you need to replace every space

                  find what: space

                  replace by: non breaking space

                  • 7. Re: replace space with non breaking space - grep
                    JoaoCP Level 2

                    Here you have a possible solution. It will need, however, two steps instead of only one. Maybe someone in the forum comes out with a more elegant approach.

                     

                    Find:

                    ([\d]+)(-[\d]+)?(\.)?\s([[:alpha:]]{2}\.)?\s?([nv]\.)\s([[:alpha:]]{3}\.)

                     

                    Replace:

                    $1$2$3~S$4~S$5~S$6

                     

                    After performing this query, you'll need to cleanup double instances of nonbreaking spaces:

                     

                    Find:

                    ~S+

                     

                    Replace:

                    ~S

                     

                     

                    If you need an explanation of what those patterns do, just ask, and I'll explain it in a following post.

                    • 8. Re: replace space with non breaking space - grep
                      Peter Spier Most Valuable Participant (Moderator)

                      I guess I fail to see why you need to replace any spaces in the above examples. Are these text strings part of longer strings that would actually break across a line? Can you be more specific about what this is?

                       

                      Indiscriminately adding non-breaking spaces can lead to impossible-to-resolve overset conditions. A.re all the cases where you need to do this (some digits or text)(space)(n. or v.)(some text)? If so I'd probably use a No Break character style instead of non-breaking spaces and apply it with something like (space)([nv]\.)(space)  where (space) can be either a typed spacebar or the Unicode value for a space and it need not be enclosed in parentheses. You could add that as a GREP style in your paragraph style definition.

                       

                      You could use the same basic expression, with the spaces in parentheses, to do a substitution to non-breaking spaces if you prefer. You can even add a look-bheind for a digit as a vaildator to prevent the change at the end of a sentence under some unlikely circumstances.

                       

                      Note that I have not tested the expression

                      • 9. Re: replace space with non breaking space - grep
                        ArchaeoJoFo Level 1

                        Thanks for the fast replies,

                         

                        sorry for beeing not specific enough but English is not my native language, too.

                        OK. I have articles about archaeology and the examples come within the phrases.

                         

                        Noch vor der Mitte des 2. Jt. v. Chr. wurde die

                        Stadtmauer errichtet, die als logistisch und administrativ

                        aufwändiges Bauvorhaben hervorsticht.

                         

                        Neue Forschungsergebnisse legen nahe, dass eine

                        autonome Form der politischen Organisation auch

                        schon in der Spätbronzezeit (ca. 1500–1200 v. Chr.)

                        bestanden haben könnte.

                         

                        And yes, the main goal is not to break these dates over lines. Could you explain how to:

                        A.re all the cases where you need to do this (some digits or text)(space)(n. or v.)(some text)? If so I'd probably use a No Break character style instead of non-breaking spaces and apply it with something like (space)([nv]\.)(space)  where (space) can be either a typed spacebar or the Unicode value for a space and it need not be enclosed in parentheses. You could add that as a GREP style in your paragraph style definition.

                         

                        You could use the same basic expression, with the spaces in parentheses, to do a substitution to non-breaking spaces if you prefer. You can even add a look-bheind for a digit as a vaildator to prevent the change at the end of a sentence under some unlikely circumstances.

                        Thanks, Chris

                        • 10. Re: replace space with non breaking space - grep
                          ArchaeoJoFo Level 1

                          Hi,

                           

                          I tested your code and waht comes up was this:

                          screen_01.JPG

                          Which means no finds. maybe I meda a mistake but I just copy/paste your code.

                          Thanks for your effort.

                          Chris

                          • 11. Re: replace space with non breaking space - grep
                            Peter Spier Most Valuable Participant (Moderator)

                            If you tell me what was unclear about the explanation it would help. Do you understand waht GREP styles are?

                             

                            the expression (space)([nv.])(space) finds a space followed by any combination of n, v and ., followed by another space. Now that I think about it, that probably won't be sufficient for the GREP style, you would need \d\s[nv.]\sC which is any digit followed by any white space, then any combination of nv., followed by any space and an upper-case C (since all your dates end in Chr.). Applying a character style to that string whose only attribute is No Break will keep the dates from breaking at the spaces. That said, you probably also need  one to prevent the break at the space after ca. and at the dash in a date range. You can do this as three independent  GREP styles using the longer qurey from just above, ca\.\s\d and \d~=\d for ca. followed by any space followed by any digit and any digit en-dash any digit, respectively.

                            • 12. Re: replace space with non breaking space - grep
                              Peter Spier Most Valuable Participant (Moderator)

                              In your screen shot above youve got the first two lines that have periods after the the digit and then intervening text, which I didn't take into account, and they well not work with the expressions I wrote.

                              • 13. Re: replace space with non breaking space - grep
                                JoaoCP Level 2

                                Sorry. I've just made a test and found out that, somehow, when I pasted code here, it gained an extra space in the Find pattern. I removed the extra space, and it worked. This is the correct version:

                                 

                                ([\d]+)([-–][\d]+)?(\.)?\s([[:alpha:]]{2}\.)?\s?([nv]\.)\s([[:alpha:]]{3}\.)

                                 

                                The extra space was in the end, after the {3} and before the \.

                                 

                                Based on your last sample, I change the part where it matches the hyphen. Besides looking only for a regular hyphen, the pattern now also looks for an en dash.

                                 

                                Both modifications are highlighted in bold, in the pattern above.

                                 

                                You can also use the pattern in a GREP style, like Peter suggested. However, for this usage, the pattern has a lot of unnecessary parentheses. But they don't hurt, either. So, you can use it in a GREP style as is.

                                 

                                If you want to try this approach, do the following:

                                 

                                1. Create a character style whose only attribute is a "No Break" option. I suggest that, temporarily, you also include a Red color in this character style, so the places where it works will be clearly visible.

                                 

                                2. In your paragraph style, choose GREP styles, then click "New GREP style". In "Apply style:", choose the No Break character style you've just created. In the "To text: " field, paste the GREP pattern above.

                                 

                                3. That's it. All the occurrences of that pattern will receive the No Break attribute, thus will not break in the end of the lines.

                                • 14. Re: replace space with non breaking space - grep
                                  Peter Spier Most Valuable Participant (Moderator)

                                  Let's back up again for just a moment, though. What. exactly is the objection to having a break within the date string, and doe it apply to the entire string, or just part? Suppose the string falls at the end of a line, but the line is just the very slightest bit too short to accommodate the whole thing, or perhaps it's too short for the last three characters. Is it  visually better to break the string or to leave a large blank hole at the end of the line of 15 characters length?

                                  • 15. Re: replace space with non breaking space - grep
                                    ArchaeoJoFo Level 1

                                    The point is, that i have strict rules not to break dates over lines from the editors. The examples in post 9 were just ment to show phrases in the articles. The textframes may be wider with more characters per line and the paragraphs are longer, too.

                                     

                                    Thanks for your help.

                                    Chris

                                    • 16. Re: replace space with non breaking space - grep
                                      Peter Spier Most Valuable Participant (Moderator)

                                      I think you should discuss this with the editors. It may make more sense to re-write a paragraph to move the date than to make an ugly book, especially if the text is justified.

                                      • 17. Re: replace space with non breaking space - grep
                                        ArchaeoJoFo Level 1

                                        That works fine for me.   Thank you for the step by step explanation.

                                        My problem is solved and I now can use and adjust the code for other things.

                                         

                                        Hava a nice week!!

                                        Chris

                                        • 18. Re: replace space with non breaking space - grep
                                          JoaoCP Level 2

                                          You're welcome! I'm glad I could help.