-1

I am writing a node.JS application to parse google takeout data from google photos, for EXIF data.

I am not good at regex, and have tried sites such as regexr.com to no avail, since there are a lot of unknowns. But I am wondering if there is a more efficient way of doing this?

Say I could have various potential strings

"IMG_20150628_184721-ANIMATION" # There is a date + time within, but they are not in an ISO format, also non-regular characters "Screenshot_2015-06-27-22-51-00" # Has a date/time at the end, but also has useless string within it "2015-06-28" # Cleanly formatted date without time "2015-06-28 22:51:05" # Cleanly formatted date and time "2015-06-28 #1" # Date + Space and Extra characters that I don't want "2015-06-28 #3 " # Date + Space and extra characters that I don't want and a trailing space "2015-06-18-19" # Date + An extra number (happens to be the next day) "NoDateOrTimeInThisString" # No Date "IMG_1234" # No date

This list is non exhaustive, and there may be other strings before+after dates. It is also possible that a date/time will not actually be in the string

I don't have any way of knowing which of these particular file names I am going to end up with, and within my knowledge scope I don't know how I would be able to extract the dates from each of these.

Does anyone have any idea how i'd go about doing this in JavaScript?Such as a library? If possible i'd like to be able to get it into a JS Date/Time object. But I have no idea how i'd go about this.

1

1 Answer 1

0

You might want to have a look at Moment.js.

Also, regexes deserve a whole tutorial by themselves, they're a language of their own. Specifically, look at capturing groups, whihc allow you to extraxt certain parts of the matched expression.

What assumptions are you allowed to make on the input? Will there always be a sequence of yyyy(...)mm(...)dd hidden in it somewhere?

You could try something like

.*([0-9]{4}).*([0-9]{2}).*([0-9]{2}).*

(Haven't tried the expression yet, may need some tweaking.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.