split long string at single numbers followed by a . not prefixed by a +

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Oliver Adams Oliver Adams 2 weeks, 1 day ago.

  • Author
    Posts
  • #68328
    Profile photo of Oliver Adams
    Oliver Adams
    Participant

    I have a very long string I need to split in several places.
    Example

    1. Distraction — Target suffers a -30 modification for all
    actions.
    2. Confusion — Target is incapable of making decisions or
    initiating action. He may continue to fight current foes or in
    self-defense with +5.
    3. Blur Vision — Target suffers a -100 OB modification for
    missile attacks, and a -50 modification for all other actions.
    4. Fear — Target fears caster and attempts to flee. Fleeing
    normally equates to moving
    at maximum pace away from
    the caster.
    5. Stumble — Target becomes
    unbalanced. If he is moving,
    he trips and falls (0% action
    for 1-5 rnds). If he is performing
    a maneuver, the
    manuever fails.

    I am trying to split this at each 1,2,3,4,5 but the rows ending with +5. cause a lot of problems. Any ideas?

    Thanks in advance.

  • #68329
    Profile photo of Sam Boutros
    Sam Boutros
    Participant
    $myString = '1. Distraction — Target suffers a -30 modification for all
    actions.
    2. Confusion — Target is incapable of making decisions or
    initiating action. He may continue to fight current foes or in
    self-defense with +5.
    3. Blur Vision — Target suffers a -100 OB modification for
    missile attacks, and a -50 modification for all other actions.
    4. Fear — Target fears caster and attempts to flee. Fleeing
    normally equates to moving
    at maximum pace away from
    the caster.
    5. Stumble — Target becomes
    unbalanced. If he is moving,
    he trips and falls (0% action
    for 1-5 rnds). If he is performing
    a maneuver, the
    manuever fails.'
    
    $myOutput = 1..5 | % { 
        $StartMarker = $myString.IndexOf("$_. ")
        if ($_ -eq 5) {
            $EndMarker = $myString.Length
        } else {
            $EndMarker = $myString.IndexOf("$($_+1). ")
        }  
        [PSCustomObject]@{
            Number = $_
            String = $myString.Substring($StartMarker+3,$EndMarker-(3+$StartMarker))
        }
    }
    
    $myOutput | FL
    

    Output:

    Number : 1
    String : Distraction — Target suffers a -30 modification for all
             actions.
             
    
    Number : 2
    String : Confusion — Target is incapable of making decisions or
             initiating action. He may continue to fight current foes or in
             self-defense with +5.
             
    
    Number : 3
    String : Blur Vision — Target suffers a -100 OB modification for
             missile attacks, and a -50 modification for all other actions.
             
    
    Number : 4
    String : Fear — Target fears caster and attempts to flee. Fleeing
             normally equates to moving
             at maximum pace away from
             the caster.
             
    
    Number : 5
    String : Stumble — Target becomes
             unbalanced. If he is moving,
             he trips and falls (0% action
             for 1-5 rnds). If he is performing
             a maneuver, the
             manuever fails.
    
  • #68334
    Profile photo of Nathan Hartley
    Nathan Hartley
    Participant

    There are many ways to do this. Here is one more:

    
    # Mocked Get-Content, so the test input can live in one file.
    function Get-Content-ment {
    @'
    1. Distraction — Target suffers a -30 modification for all
    actions.
    2. Confusion — Target is incapable of making decisions or
    initiating action. He may continue to fight current foes or in
    self-defense with +5.
    3. Blur Vision — Target suffers a -100 OB modification for
    missile attacks, and a -50 modification for all other actions.
    4. Fear — Target fears caster and attempts to flee. Fleeing
    normally equates to moving
    at maximum pace away from
    the caster.
    5. Stumble — Target becomes
    unbalanced. If he is moving,
    he trips and falls (0% action
    for 1-5 rnds). If he is performing
    a maneuver, the
    manuever fails.
    '@ -split '\r?\n'
    }
    
    
    function Parse-File {
        param (
            $Path
        )
    
        $headingRegex = '^(\d+)\.\s(.*)'
     
        $output = New-Object System.Text.StringBuilder
    
        # Change Get-Content-ment to Get-Content for this to work correctly.
        foreach ($line in (Get-Content-ment $Path)) {
            if ($line -match $headingRegex) {
                if ($output.Length -gt 0) {
                    Write-Output $output.ToString()
                    $null = $output.Clear()
                }
                $null = $output.AppendLine($Matches[2])
            }
            else {
                $null = $output.AppendLine($line)
            }
        }
        if ($output.Length -gt 0) {
                    Write-Output $output.ToString()
        }
    }
    
    $bulletPoints = Parse-File U:\temp\temp.txt
    
    $bulletPoints.Count
    
    $bulletPoints
    
    
    

    Which outputs:

    5
    
    Distraction — Target suffers a -30 modification for all
    actions.
    
    Confusion — Target is incapable of making decisions or
    initiating action. He may continue to fight current foes or in
    self-defense with +5.
    
    Blur Vision — Target suffers a -100 OB modification for
    missile attacks, and a -50 modification for all other actions.
    
    Fear — Target fears caster and attempts to flee. Fleeing
    normally equates to moving
    at maximum pace away from
    the caster.
    
    Stumble — Target becomes
    unbalanced. If he is moving,
    he trips and falls (0% action
    for 1-5 rnds). If he is performing
    a maneuver, the
    manuever fails.
    

    Did you intend there to be, carriage returns within the bullet points (numbered blocks)? As, this would be much simpler without them.

    • #68338
      Profile photo of Oliver Adams
      Oliver Adams
      Participant

      Thank you so much for such fast and well written answers. Why is everything so simple when you see an answer!

You must be logged in to reply to this topic.