2

I'm trying to migrate data from a Excel spreadsheet to user profile properties.

Data ex:

Energy & Resources;#67777;#Financial Services;#137;#Consumer Business & Transportation;#23;#Real Estate;#4

How can I format the text to be like:

Energy & Resources;Financial Services;Consumer Business & Transportation;Real Estate;

Regex: rexex example

How can I manage to get it into a array

$text = "Energy & Resources;#67;#Financial Services;#137"

$text -replace ";[#\d;]+" 

Then I get: Energy & Resources that is correct for the first value, but how do I get all in a array?

1Energy & Resources [2]Financial Services [3]Consumer Business & Transportation [4].........

1
  • did you try ";[#\d;]+/g" the /g is for global replacement Feb 19 '15 at 14:39
3

Use -replace PowerShell Regex based operator:

$line = "Energy & Resources;#67777;#Financial Services;#137;#Consumer Business & Transportation;#23;#Real Estate;#4"
$fmtLine = $line -replace ";[#\d;]+", ';'

Update

In order to return the array of values use -split operator or String.Split method, for example:

$line = "Energy & Resources;#67777;#Financial Services;#137;#Consumer Business & Transportation;#23;#Real Estate;#4"
$fmtline = $line -replace ";[#\d;]+", ';'  
$values = $fmtline.Split(';', [StringSplitOptions]::RemoveEmptyEntries) 

PowerShell: Working With Regular Expressions (regex)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.