Extract number from string

Sam White 26 Reputation points
2023-02-18T16:10:19.5533333+00:00

I have multiple multiple line strings in the below format.

$p=@'

Name=James

Location=IN

PinCode:5000112

Dept=Sales

Employee:602389

'@

I want to extract only the Employee ID from the above string, however there is no specific index location for the Employee ID i.e. it can appear anywhere on the string and there is no specific delimiter for the Employee ID in the string. I am running the below command, however it is returning both the pin code and the employee id.

$p -replace '\D+([0-9]).','$1'

How can I only extract the employee id from any such string?

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

8 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-02-19T15:54:02.23+00:00

    Your data borders on the "there is no pattern"!

    Give this regex a try:

    $p=@'
    Name=James
    Location=IN
    ID=602389
    PinCode:5000112
    Dept=Sales
    '@
    
    
    $e=""
    if ($p -match '(?m)(?(?(Employee|ID))[^A-Za-z0-9](\d+))|^(\d+)'){
        $e = $Matches[1]
    }
    $matches
    
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2023-02-21T03:22:54.08+00:00

    I don't think you saw my answer from 19 February. It will accept any character that's not a letter of digit.

    $p=@'
    Name=James
    Location=IN
    ID=602389
    PinCode:5000112
    Dept=Sales
    '@
    
    
    $e=""
    if ($p -match '(?m)(?(?(Employee|ID))[^A-Za-z0-9](\d+))|^(\d+)'){
        $e = $Matches[1]
    }
    $matches
    

  3. Rich Matheisen 47,901 Reputation points
    2023-02-22T04:09:46.69+00:00

    There's more than one way to do this: Remove the need to deal with multi-line strings! And don't complicate the regex.

    $p=@'
    Name=James
    Location=IN
    Employee:602389
    PinCode:5000112
    Dept=Sales
    '@
    
    $e=""
    foreach($l in ($p -split ("`r`n"))){
        if ($l -match '^Employee[^A-Za-z0-9]{1}(\d+)$|^ID[^A-Za-z0-9]{1}(\d+)$|^(\d+)$'){
            $e = $Matches[1]
            break
        }
    }
    $matches
    $e
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.