Home > Back-end >  Regex String confusion and string parse
Regex String confusion and string parse

Time:10-06

Previously I was show on stack a good way to parse a string by using Regex. https://regex101.com/r/BHamf0/1 was the result.

Today I need to parse the following string and find if enableCollaborativeInbox: is true or false

Group: [email protected]
  Group Settings:        
    id: <ID Num> 
    name: Dir
    description:
    directMembersCount: 0
    adminCreated: True
    allowExternalMembers: false
    allowWebPosting: true
    archiveOnly: false
    customFooterText:
    customReplyTo:
    customRolesEnabledForSettingsToBeMerged: false
    defaultMessageDenyNotificationText:
    defaultSender: DEFAULT_SELF
    enableCollaborativeInbox: false
    includeCustomFooter: false
    includeInGlobalAddressList: true
    isArchived: true
    membersCanPostAsTheGroup: true
    messageModerationLevel: MODERATE_NONE
    replyTo: REPLY_TO_IGNORE
    sendMessageDenyNotification: false
    spamModerationLevel: ALLOW
    whoCanContactOwner: ALL_MANAGERS_CAN_CONTACT
    whoCanJoin: INVITED_CAN_JOIN
    whoCanLeaveGroup: NONE_CAN_LEAVE
    whoCanPostMessage: ANYONE_CAN_POST
    whoCanViewGroup: ALL_MEMBERS_CAN_VIEW
    whoCanViewMembership: ALL_MEMBERS_CAN_VIEW
    whoCanDiscoverGroup: ALL_MEMBERS_CAN_DISCOVER
      showInGroupDirectory: false
    whoCanAssistContent: NONE
      whoCanAssignTopics: NONE
      whoCanEnterFreeFormTags: NONE
      whoCanHideAbuse: NONE
      whoCanMakeTopicsSticky: NONE
      whoCanMarkDuplicate: NONE
      whoCanMarkFavoriteReplyOnAnyTopic: NONE
      whoCanMarkNoResponseNeeded: NONE
      whoCanModifyTagsAndCategories: NONE
      whoCanTakeTopics: NONE
      whoCanUnassignTopic: NONE
      whoCanUnmarkFavoriteReplyOnAnyTopic: NONE
    whoCanModerateContent: OWNERS_AND_MANAGERS
      whoCanApproveMessages: OWNERS_AND_MANAGERS
      whoCanDeleteAnyPost: OWNERS_AND_MANAGERS
      whoCanDeleteTopics: OWNERS_AND_MANAGERS
      whoCanLockTopics: OWNERS_AND_MANAGERS
      whoCanMoveTopicsIn: OWNERS_AND_MANAGERS
      whoCanMoveTopicsOut: OWNERS_AND_MANAGERS
      whoCanPostAnnouncements: OWNERS_AND_MANAGERS
    whoCanModerateMembers: OWNERS_AND_MANAGERS
      whoCanAdd: ALL_MANAGERS_CAN_ADD
      whoCanApproveMembers: ALL_MANAGERS_CAN_APPROVE
      whoCanBanUsers: OWNERS_AND_MANAGERS
      whoCanInvite: ALL_MANAGERS_CAN_INVITE
      whoCanModifyMembers: OWNERS_AND_MANAGERS
    Deprecated:
      allowGoogleCommunication: false
      favoriteRepliesOnTop: true
      maxMessageBytes: 25M
      messageDisplayFont: DEFAULT_FONT
      whoCanAddReferences: NONE
      whoCanMarkFavoriteReplyOnOwnTopic: NONE
  Non-Editable Aliases: (1)
    alias: [email protected]
  Members: (0)
  Total Members in Group: 0

I tried to use the previous examples with https://regex101.com/r/BHamf0/1 and had no success. I am not sure why. The goal is to learn if enableCollaborativeInbox: is true or false

Pervious PS code that worked

    $regex = '(?s)^. \bCurrent: ([\d.] ). \bLatest: ([\d.] ).*$'
    $CurrentVersion, $LatestVersion = -split (($GAMCheck -join "`n") -replace $regex, '$1 $2')

And the pervious string

GAMADV-XTD3 6.07.31 - https://github.com/taers232c/GAMADV-XTD3 - pyinstaller
Ross Scroggs <[email protected]>
Python 3.9.7 64-bit final
Windows 10 10.0.18363 SP0 Multiprocessor Free AMD64
Path: C:\GAMADV-XTD3
Config File: C:\GAMConfig\gam.cfg, Section: DEFAULT, customer_id: C047encoi, domain: sonos.com        
Version Check:
  Current: 6.07.31
   Latest: 6.07.31

CodePudding user response:

Use the -match operator to test for the presence of enableCollaborativeInbox: and capture the value following it:

$s = @'
Group: [email protected]
  Group Settings:        
    id: <ID Num> 
    name: Dir
    description:
    directMembersCount: 0
    adminCreated: True
    allowExternalMembers: false
    allowWebPosting: true
    archiveOnly: false
    customFooterText:
    customReplyTo:
    customRolesEnabledForSettingsToBeMerged: false
    defaultMessageDenyNotificationText:
    defaultSender: DEFAULT_SELF
    enableCollaborativeInbox: false
    includeCustomFooter: false
    includeInGlobalAddressList: true
    isArchived: true
    membersCanPostAsTheGroup: true
    messageModerationLevel: MODERATE_NONE
    replyTo: REPLY_TO_IGNORE
    sendMessageDenyNotification: false
    spamModerationLevel: ALLOW
    whoCanContactOwner: ALL_MANAGERS_CAN_CONTACT
    whoCanJoin: INVITED_CAN_JOIN
    whoCanLeaveGroup: NONE_CAN_LEAVE
    whoCanPostMessage: ANYONE_CAN_POST
    whoCanViewGroup: ALL_MEMBERS_CAN_VIEW
    whoCanViewMembership: ALL_MEMBERS_CAN_VIEW
    whoCanDiscoverGroup: ALL_MEMBERS_CAN_DISCOVER
      showInGroupDirectory: false
    whoCanAssistContent: NONE
      whoCanAssignTopics: NONE
      whoCanEnterFreeFormTags: NONE
      whoCanHideAbuse: NONE
      whoCanMakeTopicsSticky: NONE
      whoCanMarkDuplicate: NONE
      whoCanMarkFavoriteReplyOnAnyTopic: NONE
      whoCanMarkNoResponseNeeded: NONE
      whoCanModifyTagsAndCategories: NONE
      whoCanTakeTopics: NONE
      whoCanUnassignTopic: NONE
      whoCanUnmarkFavoriteReplyOnAnyTopic: NONE
    whoCanModerateContent: OWNERS_AND_MANAGERS
      whoCanApproveMessages: OWNERS_AND_MANAGERS
      whoCanDeleteAnyPost: OWNERS_AND_MANAGERS
      whoCanDeleteTopics: OWNERS_AND_MANAGERS
      whoCanLockTopics: OWNERS_AND_MANAGERS
      whoCanMoveTopicsIn: OWNERS_AND_MANAGERS
      whoCanMoveTopicsOut: OWNERS_AND_MANAGERS
      whoCanPostAnnouncements: OWNERS_AND_MANAGERS
    whoCanModerateMembers: OWNERS_AND_MANAGERS
      whoCanAdd: ALL_MANAGERS_CAN_ADD
      whoCanApproveMembers: ALL_MANAGERS_CAN_APPROVE
      whoCanBanUsers: OWNERS_AND_MANAGERS
      whoCanInvite: ALL_MANAGERS_CAN_INVITE
      whoCanModifyMembers: OWNERS_AND_MANAGERS
    Deprecated:
      allowGoogleCommunication: false
      favoriteRepliesOnTop: true
      maxMessageBytes: 25M
      messageDisplayFont: DEFAULT_FONT
      whoCanAddReferences: NONE
      whoCanMarkFavoriteReplyOnOwnTopic: NONE
  Non-Editable Aliases: (1)
    alias: [email protected]
  Members: (0)
  Total Members in Group: 0
'@

if($s -match 'enableCollaborativeInbox:\s*(\S )'){
  [bool]::Parse($Matches[1])
} 

CodePudding user response:

Use the Match operator and a look-behind-assertion to find the label, then capture the value:

  $raw -match "(?<=enableCollaborativeInbox:\s ).*"
  $Matches[0] -eq "true"
  • Related