Powershell добавляет CR в конце группы регулярных выражений - PullRequest
0 голосов
/ 19 января 2019

Я получаю CR между совпадением с регулярным выражением и символом ','. Что происходит?

$r_date ='ExposeDateTime=([\w /:]{18,23})'   
$v2 = (Select-String -InputObject $_ -Pattern $r_date | ForEach-Object {$_.Matches.Groups[1].Value}) + ',';

Пример вывода:

9/25/2018 8:45:19 [CR],

Исходная строка:

ExposeDateTime=9/25/2018 8:45:19 AM
Error=Dap
PostKvp=106
PostMa=400
PostTime=7.2
PostMas=2.88
PostDap=0

Ответы [ 2 ]

0 голосов
/ 19 января 2019

, если ваш ввод представляет собой многострочную строку, хранящуюся в $Original, то это более простое регулярное выражение, кажется, делает эту работу.[ ухмылка ] использует именованную группу захвата и флаг регулярного выражения multiline для захвата строки после ExposedDateTime= и до до окончания следующей строки.

$Original -match '(?m)ExposeDateTime=(?<Date>.+)$'
$Matches.Date

вывод ...

9/25/2018 8:45:19 AM
0 голосов
/ 19 января 2019

Попробуйте это:

$original = @"
ExposeDateTime=9/25/2018 8:45:19 AM
Error=Dap
PostKvp=106
PostMa=400
PostTime=7.2
PostMas=2.88
PostDap=0
"@

$r_date ='ExposeDateTime=([\d\s/:]+(?:(?:A|P)M)?)'   
$v2 = (Select-String -InputObject $original -Pattern $r_date | ForEach-Object {$_.Matches.Groups[1].Value}) -join ','

Детали регулярного выражения:

ExposeDateTime=    Match the characters “ExposeDateTime=” literally
(                  Match the regular expression below and capture its match into backreference number 1
   [\d\s/:]        Match a single character present in the list below
                   A single digit 0..9
                   A whitespace character (spaces, tabs, line breaks, etc.)
                   One of the characters “/:”
      +            Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   (?:             Match the regular expression below
      (?:          Match the regular expression below
                   Match either the regular expression below (attempting the next alternative only if this one fails)
            A      Match the character “A” literally
         |         Or match regular expression number 2 below (the entire group fails if this one fails to match)
            P      Match the character “P” literally
      )
      M            Match the character “M” literally
   )?              Between zero and one times, as many times as possible, giving back as needed (greedy)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...