PowerShell Regex

Welcome Forums General PowerShell Q&A PowerShell Regex

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
2 years, 3 months ago.

  • Author
    Posts
  • #53507

    Participant
    Points: 0
    Rank: Member

    Hi All,

    Need a help to extract the exact integer number from a set of string. In the below example, I want to extract only 7614.
    Suggest a regex for the same.

    INFO: TB_SEC_FOLDER, 7614 folders

    I tried,

    $a = 'INFO: TB_SEC_FOLDER, 7614 folders'
    $a -replace '\D+(\d+)','$1'

    but it will give me output as 7614 folders, it will work if its only $a = 'INFO: TB_SEC_FOLDER, 7614'

  • #53520

    Participant
    Points: 0
    Rank: Member
    $a -replace '^\D+(\d+).*$','$1'

    The problem is that it replaces only the regex match, so you have to have the pattern match the whole string.

  • #53524

    Participant
    Points: 0
    Rank: Member

    Thanks Craig

The topic ‘PowerShell Regex’ is closed to new replies.