-1

Indesign GREP newbie here. I'm receiving text of times that requires reformating. How would I format time ranges using a GREP code to omit the first "am" or "pm" from any time ranges that have "am" or "pm" twice in the same time range? Also, I'm trying to omit the ":00" from all time ranges. Is there a simple way of eliminating that in the same GREP?

For Example:

This is what’s provided: • 9:15am-10:00am • 1:00pm-3:30pm • 11:30am-12:30pm • 9:00am-1:00pm

This is what I’m aiming for: • 9:15-10am • 1-3:30pm • 11:30am-12:30pm • 9am-1pm

  • I believe it's impossible to do with just grep search/replace. It's need a script to make quite tricky calculations and perform several replaces (with or without greps, it doesn't matter). – Yuri Khristich Dec 23 '21 at 20:07

2 Answers2

1

If you don't mind a script, here is the basic algorithm:

function clean_ranges(txt) {
  txt = txt.replace(/:00/g, '');

  var ranges = txt.split(' • ');

  for (var i=0; i<ranges.length; i++) { 
    if (ranges[i].split('am').length>2) ranges[i] = ranges[i].replace('am','');
    if (ranges[i].split('pm').length>2) ranges[i] = ranges[i].replace('pm','');
  } 

  return ranges.join(' • ');
}

var str = '• 9:15am-10:00am • 1:00pm-3:30pm • 11:30am-12:30pm • 9:00am-1:00pm';

console.log(clean_ranges(str));

The full implementation for InDesign is here:

function clean_ranges(txt) {
    txt = txt.replace(/:00/g, '');

    var ranges = txt.split(' • ');

    for (var i = 0; i < ranges.length; i++) {
        if (ranges[i].split('am').length > 2) ranges[i] = ranges[i].replace('am', '');
        if (ranges[i].split('pm').length > 2) ranges[i] = ranges[i].replace('pm', '');
    }

    return ranges.join(' • ');
}

try {
    var txt = app.selection[0].contents;
    if (txt.split('-').length < 2) exit(); // do nothing if selected text has no '-'
    app.selection[0].contents = clean_ranges(txt);
} catch(e) {}

It will replace a selected text.

RobC
  • 22,977
  • 20
  • 73
  • 80
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
0

This can be done with GREP, but you will need 2 searches:

am(?=-\d{1,2}:\d{2}am)

pm(?=-\d{1,2}:\d{2}pm)
cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31