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
  • 6,763
  • 10
  • 35
  • 34

2 Answers2

2

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

^-+(.+?)-+$
Chad Birch
  • 71,186
  • 23
  • 148
  • 148
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
  • 6,857
  • 5
  • 45
  • 59