Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using regex to replace some text

this code only replaces the last instance in a string, is there anyway to have it work on all instances in the string?

if ($syllableCount -ge 2 -and $lastthree -eq 'ing') {
        $newWord = $word -replace "ing$", "in'"
        $newWord
    }
    else {
        $word
    }

output:

.\friar.ps1 -words 'Doing making going'
Doing making goin'

PS – I am working on Doug Finkes Tiny-Powershell-Projects challenges, they are alot of fun.
text

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if ($syllableCount -ge 2 -and $lastthree -eq 'ing') {
        $newWord = $word -replace "ing$", "in'"
        $newWord
    }
    else {
        $word
    }

actual output:

.\friar.ps1 -words 'Doing making going'
Doing making goin'

Would like to see:

Doin' makin' goin'

>Solution :

Your regex just needs a tiny modification to make it work, change $ (end of string) for \b (matches a word boundary) then it should work:

'Doing making going' -replace 'ing\b', "in'"

See https://regex101.com/r/KCIHRc/1 for regex details.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading