tags:

views:

206

answers:

2

What's a regex pattern to trim the hyphens from the start and end of a string?

-----name1-name2-----

should become

name1-name2

^(-+).+(-+)$ doesn't seem to work...

+2  A: 

I would take the opposite approach, and pull the middle out like this:

^-+(.+?)-+$
Chad Birch
+1  A: 

You need to match either the beginning or the end like this:

(^-+)|(-+$)

If I try this out in PowerShell I get the following result:

PS> "-----name1-name2----" -replace "(^-+)|(-+$)", ""
name1-name2
Manga Lee