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...

Paulo
  • 7,123
  • 10
  • 37
  • 34

2 Answers2

2

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

^-+(.+?)-+$
Chad Birch
  • 73,098
  • 23
  • 151
  • 149
1

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
Magnus Lindhe
  • 7,157
  • 5
  • 48
  • 60