-2

In Google Apps Script, I am trying to parse a string with regexp. My string looks something like this:

The first part is text only but of varying length and then follows a date (dd.mm.yyyy) and a starting time (on the European 24h scale) and an ending time (also on the European 24h scale), i.e.:

Event in Cologne dd.mm.yyyy 18:30 - 23:00

What i would like to do is parse this string with regexp in Google Apps Script in four parts and save those as new variables:

  • title,
  • date,
  • start time &
  • end time

Can anyone here help me with this?

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user1582830
  • 79
  • 1
  • 10
  • [This (other) question](http://stackoverflow.com/questions/11761563/javascript-regexp-for-splitting-text-into-sentences-and-keeping-the-delimiter) is a good example of how to ask a question seeking help with a regexp. To improve your question, show the code you have so far, and how it is failing to meet your goal. – Mogsdad Oct 26 '14 at 11:35

1 Answers1

2

It may help you to experiment with your regexp. If you search for "regexp tester" you'll find many available options. For example, on @gskinner's RegExr.com there is a wealth of resources; tutorials, examples, a full cheat sheet, etc. But best, a live "lab" to try them out.

screenshot

So there's an example that will match the date portion of your source string. The event title comes before that, the time after... so even without a full matching regexp you should be easily able to break it down.

  • /([\d]+\.[\d]+\.[\d]+)/g matches date in MM.DD.YYYY or DD.MM.YYYY format (demo)
  • /((([0-1]?[0-9])|([2][0-3])):)([0-5][0-9])/g matches time (demo)
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • Thank you very much for the pointer. I was able to match the date ([\d]+\.[\d]+\.[\d]+) and the endtime ([\d]+\:[\d]+)$ But I wasn't able to match the text part (without date and time) and the starttime (I need starttime and endtime in separate variables). Can you help me with this? Thank you very much. Best, Phil – user1582830 Oct 26 '14 at 14:56
  • Once you've got the initial matches, use `replace()` to remove them from the original string, and you'll have the remainder. – Mogsdad Oct 26 '14 at 15:19