Home > Software design >  How do I make a regex to give me the name
How do I make a regex to give me the name

Time:03-25

I need to get the display name result from a command it looks like the following truncated result. It is shown as name: below

$Group = gam info <Group Email> nousers noaliases

$Group
Group: <Group Email> 
  Group Settings:
    id: 020------v1
    name: [email protected]
    description:
    directMembersCount: 3

I am pretty sure I need to do this with Regex but I am not sure how to make the regex

And then once I have the regex I am not sure how I use it to get the name from $group

CodePudding user response:

A bit more concise (and more robust):

(gam info group $GroupEmail nousers noaliases) -match '^\s name:' -replace '^. : '

CodePudding user response:

This might be useful in the future if you need to get a proper object out of that command:

function Parse-GamInfo {
    (@($input) -notmatch 'Group Settings:').foreach{
        begin { $obj = @{} }
        process {
            $prop, $val = $_.Split(':').Trim()
            $obj[$prop] = $val
        }
        end {
            [pscustomobject]$obj
        }
    }
}

Now you can pipe your command to this function to have an object that is easy to manipulate:

PS /> gam info <Group Email> nousers noaliases | Parse-GamInfo

Group         description name                   id          directMembersCount
-----         ----------- ----                   --          ------------------
<Group Email>             [email protected] 020------v1 3

CodePudding user response:

The answer is either

$GroupName = gam info group $GroupEmail nousers noaliases
$groupname = ($groupname -match 'Name:').Trim() | ConvertFrom-String -Delimiter ':' | Select-Object P2

or

$GroupName = gam info group $GroupEmail nousers noaliases
$GroupName = $GroupName -match ('Name:')
$GroupName = $GroupName.trim().Split(' ')[1]

If someone can answer better then this please do.

one line seems to work

$GroupName = ((gam info group $GroupEmail nousers noaliases) -match ('Name:')).trim().Split(' ')[1]
  • Related