Find a number after a sentence

This topic contains 12 replies, has 5 voices, and was last updated by  Paal Braathen 1 year, 10 months ago.

  • Author
  • #49267


    I have built a script that is triggered automatically whenever an event ID is registered in 'application' log.
    I used to parse (using split) information from the event message to do staff with that. Now I have a newer version of the application that is writing to the event log and more info is in the message and thus the parsing breaks.
    I am looking to find a better way to parse out the info that won't break anymore.
    For instance:
    I am looking to pull a number after the bellow specific sentence.
    Lets say:
    $Data = $Event[0].Message # is the message data of the first appearance

    I need to find the number inside this sentence that will always appear (just the number changes) but I don't want to use split for spaces and choose the [6] as this changes.
    Note: there are more numbers in $data I just need the one inside this sentence.
    "A message with sequence number 17 has been sent"

    Any ideas?

  • #49271

    Daniel Krebs

    I think using a regular expression with a named capture group could help here.

    if ($Event[0].Message -match 'sequence number (?\d+)')
        'Sequence number found: {0}' -f $Matches.SequenceNumber

    I hope that helps.

    Daniel Krebs

  • #49273

    random commandline

    Void is used to omit the Boolean output.

    [void]($Event[0].Message -match "number (?'num'\d+)") ; $Matches['num']
  • #49275


    Hi Daniel,
    I tried the regex you suggested on an event that contains the following "A message with sequence number 18 has been sent".
    I need to assign the number '18' to a var.

    I did that: (added square brackets)

    PS C:\> $b -match 'sequence number ([?\d+])'

    How should I pull the number?

  • #49277

    Daniel Krebs

    You've got two options to get the number. Please don't add a square bracket to the regular expression pattern because some characters have a special meaning. You can learn about regular expressions here:

    I hope that helps.

    Update: Using a Gist instead of embedded code.

    • This reply was modified 1 year, 10 months ago by  Daniel Krebs.
  • #49280

    Daniel Krebs

    Unfortunately the forum software removed some characters from the code I've embedded.

    Please check below to see a valid example:

  • #49282

    Dan Potter

    why don't you want to split the spaces? Seems obvious in the sentence to do so.

    Without regex it's as simple as

    'one two 3 four'.split() | ? {$_ -as [int]}

    • #49301


      Hi Dan,
      In my initial script I used split to spaces and picked the right position, but as I said the whole message has changed and added more words so I need a bullet proof to catch the number I need. The whole event log message has few number in it, I only need the on e that has the sentence prefixed I mentioned.

  • #49299


    PS C:\> ($b -match "A message with sequence number ([?'num'\d++])")
    PS C:\> ($b -match "A message with sequence number (?'num'\d++)")
    parsing "A message with sequence number (?'num'\d++)" – Nested quantifier +.
    At line:1 char:1
    + ($b -match "A message with sequence number (?'num'\d++)")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

    I got 2 Issues:
    1) I tried the regex without the [] but I get the above error.
    2) When I try with the [] and pipe $matches I get a wrong number '1' instead of '18'
    PS C:\Users\oferg> [void]($b -match "A message with sequence number ([?'num'\d++])"); $Matches

    Name Value
    —- —–
    1 1
    0 A message with sequence number 1

    Sorry for nagging or not getting the right picture.

    • #49407

      Paal Braathen

      What is the string you need to search through? Would this be an example: "sequence number [17]"?

      I would go for regexp as suggested, but brackets and other characters have special meaning.

      $String = "sequence number [17]"
      if ($String -match "sequence number \[(\d+)\]") {
          Write-Host $Matches[1]

      This will print the number 17. If you do not understand the above code you could read more about regexp here (as suggested):

  • #49312

    Dan Potter

    Just the same only a tad longer.

    ("A message with sequence number 17 has been sent" -split 'sequence number ')[1].split() | ? {$_ -as [int]}

  • #49314

    Dan Potter

    Here with more text for demonstration. String manipulation doesn't have to be complicated.

    $event = @"
    this is my full message
    A message with sequence number 17 has been sent
    end message 
    ($event -split 'sequence number ')[1].split() | ? {$_ -as [int]}
    • #49338


      Hi Dan,
      That really helped thank you very much I appreciate this.
      And thanks for all other here.

You must be logged in to reply to this topic.