11 Replies Latest reply on Aug 19, 2014 10:14 AM by Kai Rübsamen

    GREP, creating spaces

    öNë

      Hi everyone,

       

      I have a lot of info from a DB from ACCESS and I'm importing it to inDesign as a CSV.

       

      My problem is about the prices. All the prices come without spaces between, like this:

       

      9000000

       

      and I need to do this:

       

      9 000 000

       

      Only put the spaces. But I need to do this automatically.

       

      Someone has any idea? I'm stuck.

        • 1. Re: GREP, creating spaces
          Chinnadk Level 4

          Try this in grep find and change,

           

          Find What : \d(?=(\d\d\d)+(?!\d))

          Change To : $0 (put a space after the $0)

          • 2. Re: Re: GREP, creating spaces
            Trevorׅ Adobe Community Professional

            Chinnadk, I don't think your Greps is correct.

            I think this is correct, but there might be a better way

            app.findGrepPreferences = app.changeGrepPreferences = null;
            app.findGrepPreferences.findWhat = "(\\d)(?=\\d\\d\\d)(\\d\\d\\d)\\b";
            app.changeGrepPreferences.changeTo = "$1 $2";
            while (app.activeDocument.changeGrep ().length);
            

             

            Trevor

            • 3. Re: GREP, creating spaces
              Kai Rübsamen Adobe Community Professional

              Hi Trevor, you are wrong ;-)

               

              Chinnas Grep is the right one!

               

              Chinna is searching for a digit, if then comes packages with 3 digits. The power here is the 'plus'-symbol.

              This way with the repeater was first mentioned by Jeffrey Friedl and he writes it in this way: \d(?=(\d{3}+)\b)

               

              If you need to find all numbers without numbers of 4 digits you can search with:

              (?<=\d)\d(?=\d{3}\b)|\d(?=(\d{3}){2,}\b)

               

              Your search is limited, because there is no repeater.

               

              – Kai

              • 4. Re: GREP, creating spaces
                [Jongware] Most Valuable Participant

                Kai, a better repeater would be this:

                 

                (?=(\d{3})+\b)

                 

                Here is an alternative: don't insert spaces at all

                 

                Create a character style called "DigitSpace", set its tracking to +250 (possibly slightly less, as it depends on both font and taste), and apply it with the following GREP style:

                 

                (?=\d(\d{3})+\b)\d

                 

                This is what you get; I added an underline to show which digit gets the tracking applied:

                 

                spaced_numbers.PNG

                • 5. Re: GREP, creating spaces
                  Kai Rübsamen Adobe Community Professional

                  Hi Jongware,

                   

                  thanks for your queries. My query is simply nonsense, because of one parentheses at the wrong place. This should be the right one:  \d(?=(\d{3})+\b)


                  Do you see a benefit, if we use:

                  (?=\d(\d{3})+\b)\d instead of

                  \d(?=(\d{3})+\b)  


                  ?


                  I know, that a string can be searched twice with yours, but I see no benefit at the moment?


                  Otherwise I like the idea of applying the character style instead of inserting whitespace, because no additional characters are needed and it is dynamic.

                  1 person found this helpful
                  • 6. Re: GREP, creating spaces
                    Trevorׅ Adobe Community Professional

                    Hi Kai,

                     

                    You are wrong and so is Chinna, so is Jongware,  I am right ;-) (I think!)

                     

                    You all are not taking into account that we are talking about a csv file i.e. a table which means that their will probably be no character after the digits. :-)

                    In such a case all your versions will fail. see the screen shots.  I cant find a case where mine will fail.  I like Jongwares idea of not putting in the spaces and the underlining makes things really pretty (will also help keep the digits on the same line).

                     

                    Any ideas how to do it without the loop (I haven't thought about it too much).

                    Orig_Untitled-7 @ 125%_2014-08-19_15-30-51.pngK_andC__Untitled-7 @ 125%_2014-08-19_15-42-01.pngJongware_Untitled-7 @ 125%_2014-08-19_15-24-57.pngTrevor_Untitled-7 @ 125%_2014-08-19_15-32-11.png

                    P.s. I'm a bit lazy about using repeaters when I can use /d/d/d depends on the case and my mood

                    • 7. Re: GREP, creating spaces
                      Kai Rübsamen Adobe Community Professional

                      Argh! I forgot the table … You are right .

                      I didn’t also notice 'while (app.activeDocument.changeGrep ().length); ' because if I use only your grep in the ui, I got not the endresult.

                       

                      So to find e.g. 1000000000 we need 3 loops. Thanks for your code.

                       

                      To do it without a loop, I did in the past something like:

                      Find what: (?<=\d{3})\d\b

                      Change to: $0~<

                       

                      and then run my previous grep. But in this case, I would select the table before. So not really better.

                      • 8. Re: Re: GREP, creating spaces
                        Trevorׅ Adobe Community Professional

                        Ok

                         

                        This might be the best way, combo job, no loop and using tracking instead of spaces.

                         

                        app.doScript (addGaps, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Add Gaps to Numbers");
                        function addGaps () {
                            app.findGrepPreferences = app.changeGrepPreferences = null;
                            app.findGrepPreferences.findWhat = "(\\d{4,}\\b)";
                            app.changeGrepPreferences.changeTo = "$0 TrEvoR";
                            app.changeGrepPreferences.tracking = 0;
                            app.activeDocument.changeGrep();
                            app.findGrepPreferences.findWhat = "(?=\\d(\\d{3})+\\b)\\d";
                            app.changeGrepPreferences.tracking = 250;
                            app.changeGrepPreferences.underline = true; // for demo remove is you don't like the look
                            app.changeGrepPreferences.underlineWeight = "1pt"; // for demo remove is you don't like the look
                            app.changeGrepPreferences.underlineColor = app.colors.itemByName ("Magenta"); // for demo remove is you don't like the look
                            app.changeGrepPreferences.changeTo = "$0";
                            app.activeDocument.changeGrep();
                            app.findGrepPreferences = app.changeGrepPreferences = null;
                            app.findGrepPreferences.findWhat = " TrEvoR"; // could use findText and set the caseSensitive to True but ....
                            app.activeDocument.changeGrep();
                            app.findGrepPreferences = app.changeGrepPreferences = null;
                        }
                        

                         

                        J&amp;T_Untitled-7 @ 125%_2014-08-19_18-55-18.png

                        TrEvoR

                        • 9. Re: Re: GREP, creating spaces
                          Trevorׅ Adobe Community Professional

                          P.s. Kai, didn't see your post above but TrEvoR is more unique than ~< so no need to select the table

                          • 10. Re: GREP, creating spaces
                            öNë Level 1

                            Thank you so much you all for the help!

                             

                            I'm really happy with the results! it works!

                             

                             

                            • 11. Re: GREP, creating spaces
                              Kai Rübsamen Adobe Community Professional

                              Hi Trevor,

                               

                              great solution. Thanks!

                              I think, the second 'changeTo' isn’t necessary, since you apply some formatting.