简体   繁体   中英

Powershell - Should take only set of numbers from file name

I have a script that read a file name from path location and then he takes only the numbers and do something with them. Its working fine until I encounter with this situation.

For an example:

For the file name Patch_1348968.vip it takes the number 1348968 . In the case the file name is Patch_1348968_v1.zip it takes the number 13489681 that is wrong.

I am using this to fetch the numbers. In general it always start with patch_#####.vip with 7-8 digits so I want to take only the digits before any sign like _ or - .

$PatchNumber = $file.Name -replace "[^0-9]" , ''

I suggest to use -match instead, so you don't have to think inverted:

if( $file.Name -match '\d+' ) {
    $PatchNumber = $matches[0]
}

\d+ matches the first consecutive sequence of digits. The automatic variable $matches contains the full match at index 0, if the -match operator successfully matched the input string against the pattern.

If you want to be more specific, you could use a more complex pattern and extract the desired sub string using a capture group :

if( $file.Name -match '^Patch_(\d+)' ) {
    $PatchNumber = $matches[1]
}

Here, the anchor ^ makes sure the match starts at the beginning of the input string, then Patch_ gets matched literally (case-insensitive), followed by a group of consecutive digits which gets captured () and can be extracted using $matches[1] .

You can get an even more detailed explanation of the RegEx and the ability to experiment with it at regex101.com .

You can use

$PatchNumber = $file.Name -replace '.*[-_](\d+).*', '$1'

See the regex demo .

Details :

  • .* - any chars other than newline char as many as possible
  • [-_] - a - or _
  • (\d+) - Group 1 ( $1 ): one or more digits
  • .* - any chars other than newline char as many as possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM