none
How to code this powershell? RRS feed

  • Question

  • Referring to following coding,

    $Data = Get-Content 'D:\Sample.txt'
    $Sample = [regex]::match($Data, '""tips"":""(.*?)found').Groups[1].Value
    Write-Host $Sample, which show nothing

    $Data = Get-Content 'D:\Sample.txt'
    $Sample = [regex]::match($Data, 'tips(.*?)found').Groups[1].Value
    Write-Host $Sample, which show ","tips":"38980 items as result

    Does anyone have any suggestions?
    Thanks in advance for any suggestions

    D:\Sample.txt contain following texts

    #"nt_resulttips","tips":"38966 items found for


    Thanks in advance for any suggestions

    Saturday, January 6, 2018 12:20 PM

Answers

  • PS > $data = '#"nt_resulttips","tips":"38966 items found for'
    PS > if($data -match '"tips":"(\d+)\b'){$matches[1]}else{'not found'}
    38966


    \_(ツ)_/

    • Marked as answer by oemMicrosoft Sunday, January 7, 2018 1:00 AM
    Saturday, January 6, 2018 2:52 PM

All replies

  • You might take a look at the following sites to understand how to work with regular expressions in general and in partiucular in Powershell

    https://en.wikipedia.org/wiki/Regular_expression

    Windows Powershell Writing Regular Expressions

    https://www.regular-expressions.info/powershell.htmlAbout Comparison Operators


    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Saturday, January 6, 2018 1:10 PM
  • PS > $data = '#"nt_resulttips","tips":"38966 items found for'
    PS > if($data -match '"tips":"(\d+)\b'){$matches[1]}else{'not found'}
    38966


    \_(ツ)_/

    • Marked as answer by oemMicrosoft Sunday, January 7, 2018 1:00 AM
    Saturday, January 6, 2018 2:52 PM
  • I can find what the symbols mean, but don't understand on how think works with "(", "+",")".

    Could you please describe on what (\d+)\b represent?
    Thanks, to everyone very much for any suggestions (^v^)

    \b Matches a zero-width boundary between a word-class character (see next) and either a non-word class character or an edge

    \d matches any digit character.


    Thanks in advance for any suggestions

    Saturday, January 6, 2018 4:04 PM
  • Could you please describe on what (\d+)\b represent?
    Thanks, to everyone very much for any suggestions (^v^)

    \b Matches a zero-width boundary between a word-class character (see next) and either a non-word class character or an edge

    \d matches any digit character.


    Thanks in advance for any suggestions

    You will need to study and understand RegEx to know what that means.  You can learn RegEx in most languages as it is used all over the world.


    \_(ツ)_/

    Saturday, January 6, 2018 4:09 PM