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
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
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
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
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