4 Replies Latest reply on Jul 24, 2013 5:34 AM by c_nolte

    GREP: date/time search and replace

    c_nolte

      Hi everybody,

       

      I frequently import XML files to InDesign. The XML structure always contains the following information among others:

      start date

      start time

      end date

      end time

       

      For example, it looks like this:

       

      21.08.2013

      09:00

      25.08.2013

      18:00

       

      Now I need to re-arrange these fields in one row, just like this:

      21.08.2013, 09:00 - 25.08.2013, 18:00

       

      I already found out how to do this via GREP, but I need to go one step further:

      If "start date" and "end date" are the same (for example both are "21.08.2013"), I would like to skip the "end date" in the output. It should look like this:

      21.08.2013, 09:00 - 18:00

       

      Is there a way to do this via GREP?

       

      Thanks in advance!

        • 1. Re: GREP: date/time search and replace
          Trevorׅ Adobe Community Professional

          This will do it

           

          Trevor

           

          // By Trevor http://forums.adobe.com/thread/1261365?tstart=0
          app.doScript("formatDates ()", ScriptLanguage.javascript, undefined, UndoModes.ENTIRE_SCRIPT, "GREP: date/time search and replace");
          function formatDates ()
              {
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = "\\d\\d\.\\d\\d\.\\d\\d\\d\\d\r\\d\\d:\\d\\d\r\\d\\d\.\\d\\d\.\\d\\d\\d\\d\r(?=\\d\\d:\\d\\d\r)";
                  var doc = app.activeDocument,
                        dateFinds = doc.findGrep(),
                        l = dateFinds.length, dates;
                  while (l--)
                      {
                          app.findGrepPreferences.findWhat = "(\\d\\d\.\\d\\d\.\\d\\d\\d\\d)";
                          dates = dateFinds[l].findGrep();
                          app.findGrepPreferences.findWhat = "(\\d\\d\.\\d\\d\.\\d\\d\\d\\d)\r(\\d\\d:\\d\\d)\r(\\d\\d\.\\d\\d\.\\d\\d\\d\\d)\r";
                          if (dates[0].contents == dates[1].contents) app.changeGrepPreferences.changeTo = "$1 $2 - ";
                          else app.changeGrepPreferences.changeTo = "$1 $2 - $3 ";
                          dateFinds[l].changeGrep();
                      }
              }
          
          • 2. Re: GREP: date/time search and replace
            c_nolte Level 1

            Great! Thank you very much!

            • 3. Re: GREP: date/time search and replace
              Trevorׅ Adobe Community Professional

              Pleasure

               

              Can you mark the answere as  correct instead of a helpful