How to find a certain word in a file name and get some characters after it?
For example:
File 01 - Vertical - Day 04 - Model 01-01
File 02 - Model 01-02 - Day 02
How forever find the word Model
in a file name and extract the 5 characters that come after it, i.e. XX-XX
?
I believe that after finding a way to search for the word Model
I can use .substring()
to get the characters I want, but I haven't found an exact way to do that, since the place of the word Model
doesn't follow a pattern for me can use -Split
.
CodePudding user response:
Personally I would go with regex and named capture (though note the (in)famous quote - "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems."):
PS C:\windows\system32> 'File 02 - Model 01-02 - Day 02' -match 'Model (?<model>\d -\d )'
True
PS C:\windows\system32> $Matches.model
01-02
Regex - Model (?<model>\d -\d )'
, regex explanation @regex101:
Model
matches the characters Model literally (case sensitive)
1st Capturing Group(\d -\d )
\d
matches a digit zero through nine in any script except ideographic scripts (equivalent to \p{Nd})
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
-
matches the character-
with index 4510 (2D16 or 558) literally (case sensitive)