Отвечено Regular expression - [regex]

  • martedì 8 maggio 2012 07:39
     
     

    Hello scripting fans,

    I neer your help. I need to select string from string below.

    $mail=@{mail=peter.novak@concon.com}

    I need to get only string

    $prefix="peter.novak"

    I have tried to use [regex] but I was not succesfull, becasue I dont know what expression should be there for selecting string.

    Thanks

    Libor


    Liibas

Tutte le risposte

  • martedì 8 maggio 2012 07:46
     
     Con risposta Contiene codice

    This does the trick:

    "mail=peter.novak@concon.com" | foreach-object { [regex]::split($_,'^mail=|@.+$')[1] }

    To explain what it does, it matches on mail= and @, and splits based on that. By using the [1] you select the value of Peter Novak. If you would omit the [1] you would get two empty lines before and after peter.novak.


    • Modificato Jaap Brasser martedì 8 maggio 2012 07:48 more information
    • Contrassegnato come risposta Liibas martedì 8 maggio 2012 07:49
    • Contrassegno come risposta annullato Liibas martedì 8 maggio 2012 07:50
    • Contrassegnato come risposta Liibas martedì 8 maggio 2012 08:03
    •  
  • martedì 8 maggio 2012 08:04
     
     
    It works 

    $mail | foreach-object { ([regex]::split($_,'^@{mail=|@'))[1] }

    Thanks Jaap


    Liibas

  • martedì 8 maggio 2012 08:06
     
      Contiene codice

    Hello Liibas,

    I am not sure what you are trying to do exactly, the emailaddress is not a standard AD attribute, are you using custom AD attributes? Also what is the background of what you are trying to do?

    Since you mention using get-aduser here is an example of get-aduser to extract the mail attribute and then use regex to select the username:

    get-aduser jaapbrasser -property mail | select-object -expandproperty mail | 
    foreach-object { ([regex]::split($_,'@.+$'))[0] }