0

I'm looking for some regex help on the following:

{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }

I need to be able to just pull the start value from what is above. Ex: 2019-10-21T15:00:00Z

My regex is terrible and I don't even really have any semi-functional code to share.

Any help would be appreciated.

2
  • You need a JSON parser, not regex. Commented Oct 17, 2019 at 11:50
  • Thanks @TimBiegeleisen, you are absolutely right. Commented Oct 17, 2019 at 12:04

2 Answers 2

2

You didn't specify what tool you are using. Regex works slightly differently in Python, Perl, JavaScript, grep, etc.

For Perl, you need: Start: .\K[0-9TZ:-]+

To test this on the command-line:

echo "{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }" |grep -Po 'Start: .\K[0-9TZ:-]+'
Sign up to request clarification or add additional context in comments.

3 Comments

It would be better to use a JSON parser here.
@TimBiegeleisen - I don't think we have enough information to make that determination. As part of a larger or import project - yes, I agree. As a one-off or proof-of-concept, a JSON parser may be overkill. Also, it was pointed out elsewhere that this is not proper JSON.
Fair enough +1, I commented before that information was known. In any case, many JSON parsers can tolerate non ideal JSON to some extent.
1

Couple of things

this is not valid JSON.

{ Start: '2019-10-21T15:00:00Z', End: '2019-10-21T15:30:00Z' }

This is valid JSON.

'{ "Start": "2019-10-21T15:00:00Z", "End": "2019-10-21T15:30:00Z" }'

You can parse this using JSON.parse(), which will parse your string into a javascript object.

var jsonString = '{ "Start": "2019-10-21T15:00:00Z", "End": "2019-10-21T15:30:00Z" }';

var parsedJson = JSON.parse(jsonString);
console.log(parsedJson.Start);
console.log(parsedJson.End);

2 Comments

thanks Malcor, I agree what i provided is not valid, but that is what is being returned when i console.log it. So somehow i need to be able to parse what is being passed. { Start: '2019-10-22T15:00:00Z', End: '2019-10-22T15:30:00Z' }
If that's the output of console.log then it's already parsed. That's a typical javascript object. You should be able to access the Start and End properties.

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.