Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get the date out of this string?

PowerShell 5.1

How do I get the date out of this string? Are those delimiters pretty good to use?

'blah blah ???5/28/24 15:55??? blah blah blah' -match '???.*\s.*???'
$Matches

I was hoping this would do it:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

-match '???.*\s.*???'

>Solution :

You need to escape the ? as it is a special regex character (quantifier), what I imagine you wanted was \?\?\?.*\s.*\?\?\? or simplified \?{3}.*\s.*\?{3} and if you wanted to capture what is in between the ??? then you would need a capturing group, so:

'blah blah ???5/28/24 15:55??? blah blah blah' -match '\?{3}(.*\s.*)\?{3}'
$Matches[1] # => 5/28/24 15:55

See https://regex101.com/r/0DgImA/1 for regex details.

Another option that might work for your use case is using a lookbehind assertion:

'blah blah ???5/28/24 15:55??? blah blah blah' -match '(?<=\?{3})[^?]+'
$Matches[0] # => 5/28/24 15:55

See https://regex101.com/r/n7g4qP/1 for regex details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading