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月21日T15:00:00Z
My regex is terrible and I don't even really have any semi-functional code to share.
Any help would be appreciated.
-
You need a JSON parser, not regex.Tim Biegeleisen– Tim Biegeleisen2019年10月17日 11:50:45 +00:00Commented Oct 17, 2019 at 11:50
-
Thanks @TimBiegeleisen, you are absolutely right.javapatriot– javapatriot2019年10月17日 12:04:49 +00:00Commented Oct 17, 2019 at 12:04
2 Answers 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:-]+'
3 Comments
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
console.log then it's already parsed. That's a typical javascript object. You should be able to access the Start and End properties.