Extracting specific data from a string with regex using Powershell RRS feed

  • Question

  • Im returning some data like this in powershell:




    But i want an output like this:

    1)1 Open


    1 Open

    2 Close

    6 pending


    5 Closed

    Any help would be appreciated as my powershell isnt that good. Thanks

    Wednesday, March 12, 2014 4:24 AM


All replies

  • You need to put some effort into asking your question.

    Remember, we have no information other than what you have posted here, we can't read your mind, and we can't see your screen.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, March 12, 2014 2:19 PM
  • Hi Bill,
           Sorry for that.I'm returning some data (from the original post)  from the sharepoint list this way:

    $lookupitem = $lookupList.Items
    	$CMRSItems = $list.Items | where {$_['ID'] -le 4}
    	$CMRSItems | ForEach-Object {
    	$realval =  $_['Event TypeOld']
    	Write-Host "Number: " $_['RefID']
    	Write-Host "Status Type: "$_['Event TypeOld']

    And all i want to find out is how i can extract the strings i need to get this values like i stated in my otiginal post.

    Would appreciate if you can help.

    Wednesday, March 12, 2014 10:34 PM
  • It is very hard to se how you get the original strings from the code you just posted.

    THe best guess I have is to split the string at the semi-colon.


    Wednesday, March 12, 2014 11:54 PM
  • Write-Host writes to the console only. You cannot redirect data written to the console with Write-Host.

    The answer is not to use Write-Host at all and simply output the data you want to a file.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 1:35 AM
  • Thanks.
    I have tried using split with the semicolon(see below). But still stuck on how to get this value like this
    1 Open

    Want "1" and "Open"  separately

    $split = $t.Split(";")
    $array = @($split)
    #Write-Host $array.Count
        foreach ($i in $array) {
    		Write-Host $i

    Not the output im getting is:


    on a separate line.


    Thursday, March 13, 2014 4:58 AM
  • $t.Split(";")[0]

    I think you should start by learning the very basics of PowerShell.  Guessing will not work.

    Use the learning resources here to learn basic PowerShell.


    Thursday, March 13, 2014 5:06 AM
  • I think i tried that. Will see how i go

    Don't use powershell everyday unfortunately :(

    Where is the learning resources here?

    Thanks alot

    Thursday, March 13, 2014 6:25 AM
  • Thursday, March 13, 2014 6:41 AM
  • I think jrv is on target. Guessing won't be helpful and you will probably be frustrated.

    PowerShell requires a little bit different way of thinking than other kinds of scripting languages in many ways.

    You will save a lot of time in the long run by learning better how PowerShell works. This will also help you to ask a better question. Right now your question is very vague and it seems like your question is based on guesswork.


    -- Bill Stewart [Bill_Stewart]

    Thursday, March 13, 2014 2:11 PM
  • Thanks guys I taught I would be able to get a quick help. As I said don't use PowerShell often.

    But getting there

    This current task is just quite complicated what i'm trying to do is to set a lookup column with  multi values to a sharepoint field.

    What I have in the old list is something like this : 


    As this was a migrated list.
    to set values in a lookup field I need something like this from that string:
    LookupID which is 1 and LookUp item name which is Open
    and then "2' and "close" etc...

    Cheers guys

    Friday, March 14, 2014 12:01 AM
  • Well you certainly have proven that you do not know PowerShell.  Along with that you have proven that you do not kown anything about data or computers and computing.

    From all of you posts and the quesiotns it is impossible to understand what you are asking.

    Start by asking a question without any reference to an technology.  What is it that you are trying to do in plan language?


    Friday, March 14, 2014 12:58 AM
  • You may not learn much from this, since regular expressions are kind of like hieroglyphics, but here's one way of using regex to transform the string you mentioned:

    $string = 'Open;#1;#Close;#2;#pending;#6'
    $pattern = '(?<=^|;)#?(?<Word>[^;]+);#(?<Number>\d+)'
    foreach ($match in [regex]::Matches($string, $pattern))
        $number = $match.Groups['Number'].Value
        $word   = $match.Groups['Word'].Value
        "$number $word"

    In this case, simpler methods would work alright as well.  You could use Split and a for loop, for example, and the code would be easier to read and understand:

    $string = 'Open;#1;#Close;#2;#pending;#6'
    $array = $string.Split(';')
    for ($i = 0; $i -lt $array.Count - 1; $i += 2)
        $word = $array[$i].Trim('#')
        $number = $array[$i+1].Trim('#')
        "$number $word"

    Personal preference, there.  Regex frequently lets you get complex parsing done with very few lines of code, but in this case, it's about the same effort either way.

    Friday, March 14, 2014 12:58 AM
  • Thanks David :)
    I agree i'm not proficient with powershell especially with regex which i reckon would be quicker
    I was almost getting there(using the 2nd option you used) and i'm sure will at a point in time but thanks for the help.

    Sure will give you a help to sometime and will remind you.

    Friday, March 14, 2014 3:40 AM