You can use
$dateTimeString = [regex]::Match($line, 'd{4}-dd-ddsdd:dd:dd').Value
Your [regex]::Matches($line, 'dddd-dd-ddsdd:dd:dd')[0].Groups[1].Value
code matches a datetime substring into Group 0. You are trying to get Group 1 value by using .Groups[1].Value
, but there is no capturing group defined in the pattern.
Note there is no need using Matches
that fetches all occurrences in the string since all you need is the first match, which can be done with a mere Match
.
Also, repeating d
four times in a regex is really redundant, you may use a {4}
limiting quantifier.
If you want to only find a match at the start of the string, add ^
at the beginning: '^d{4}-dd-ddsdd:dd:dd'
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…