1

I have the following code:

$TodayDate = Get-Date -Format "dd-MM-yyyy"
$Student = Student01 - Project 01-02 - $TodayDate

Write-Host -NoNewline -ForegroundColor White "$Student"; Write-Host -ForegroundColor Green " - was delivered!"

This script returns in the console:

Student01 - Project 01-02 - dd-MM-yyyy - was delivered

How is it possible to return only everything after the first "-"?, that is Project 01-02 - dd-MM-yyyy - was delivered?

I thought about using .split, but I couldn't make it work so that it returns everything after the first "-".

3
  • 3
    You could use -Split to split it in 2, then grab the 2nd string after -. -Replace could also work here. Can you update your post with your attempts at this? Commented Dec 3, 2022 at 18:09
  • I couldn't get it to work with -split, but I used .Replace and it worked, the final script looked like this: Write-Host -NoNewline -ForegroundColor White "$(($Student).Replace("Student01 -", "$null"))"; Write-Host -ForegroundColor Green " - was delivered!" Commented Dec 3, 2022 at 19:05
  • $student = "Student01 - Project 01-02 - dd-MM-yyyy - was delivered" $index = $student.IndexOf("-") $student = $student.Substring($index + 1).Trim() Write-Host $student Commented Dec 3, 2022 at 19:26

1 Answer 1

2

Your problem boils down to wanting to remove a prefix from a string.

Given that the prefix to remove cannot be defined as a static, literal string, but is defined by the (included) first occurrence of a separator, you have two PowerShell-idiomatic options:

  • Use the -split operator, which allows you to limit the number of resulting tokens; if you limit them to 2, everything after the first separator is returned as-is; thus, the 2nd (and last) token (accessible by index [-1]) then contains the suffix of interest:

    $str = 'Student01 - Project 01-02 - dd-MM-yyyy - was delivered'
    
    # Split by ' - ', and return at most 2 tokens, then extract the 2nd (last) token.
    # -> 'Project 01-02 - dd-MM-yyyy - was delivered'
    ($str -split ' - ', 2)[-1]
    
  • Use the -replace operator, which (like -split by default) is regex-based, and allows you to formulate a pattern that matches any prefix up to and including the first separator, which can then be removed:

    $str = 'Student01 - Project 01-02 - dd-MM-yyyy - was delivered'
    
    # Match everything up to the *first* ' - ', and remove it.
    # Note that not specifying a *replacement string* (second RHS operator)
    # implicitly uses '' and therefore *removes* what was matched.
    # -> 'Project 01-02 - dd-MM-yyyy - was delivered'
    $str -replace '^.+? - '
    
    • For an explanation of the regex (^.+? - ) and the ability to experiment with it, see this regex101.com page.
Sign up to request clarification or add additional context in comments.

2 Comments

Your explanation could not have been better! I'm a beginner, especially in the PowerShell language, so I always like to understand the reason for things, that way I can evolve. I didn't know about the application of the regex (^.+? - ), I found it very interesting, it makes the job a lot easier and increases the possibilities. I also really liked the website you indicated, I didn't know it, I'll probably use it a lot. Thank you very much for your attention and patience!
I'm glad to hear it helped, and thanks for the nice feedback, @TyroneHirt. Yes. regex101.com is a great site, both for studying and troubleshooting regular expressions (regexes). Note that you'll have to tweak the defaults to make it work like PowerShell, as described in this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.