Я не думаю, что есть элегантный способ сделать это, но это можно сделать:
require "csv"
# Create a stream using the original file.
# Don't use `textmode` since it generates a problem when using this approach.
file = File.open "file.csv"
# Consume the first CSV row.
# `\r` is my row separator character. Verify your file to see if it's the same one.
loop { break if file.readchar == "\r" }
# Create your CSV object using the remainder of the stream.
csv = CSV.new file, headers: true
Где заполнены переменные $ 1, $ 2 и $ 3?
Ваше регулярное выражение работает нормально, см. Этот код:
$temp = "toLocRobo_2019-01-30"
$regex = [regex]::Matches($temp, '([0-9]+)-([0-9]+)-([0-9]+)')
$regex.groups[0].value
$regex.groups[1].value
$regex.groups[2].value
$regex.groups[3].value
Вы должны использовать $1
и т. Д. При выполнении действий замены. Чтобы просто сопоставить дату, я бы сделал что-то вроде этого:
$temp = "toLocRobo_2019-01-30"
# using named groups
if ($temp -match '(?<year>\d{4})-(?<month>\d{1,2})-(?<day>\d{1,2})') {
Write-Output ("year: {0}, month: {1}, day: {2}" -f $matches['year'],$matches['month'],$matches['day'])
}
# using numbered groups
if ($temp -match '(\d{4})-(\d{1,2})-(\d{1,2})') {
Write-Output ("year: {0}, month: {1}, day: {2}" -f $matches[1],$matches[2],$matches[3])
}