0

I want a dash to be added at the end and beginning of a string.

Example:

"chicken" -> "-chicken-"  
"nuggets" -> "-nuggets-"  

So I'm pretty much asking for Regexes.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Julian Avar
  • 476
  • 1
  • 5
  • 24
  • `sed s/.*/-&-/`. At least try next time. - Replace `.` with `[^ \t]` if you define a string as stuff between whitespace. – ShellFish Jun 15 '15 at 19:48

2 Answers2

1

You can use the following to match:

^|$

And replace with - (dash)

See DEMO

PS: Use it with m (multiline) flag.

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
1

The regex is:

/^(.*)$/-\1-/

But why don't you add dashes without regex? I mean, why not to use string operators? Like str = '-' + str +'-'; or $str = "-" . $str . "-", etc. depending on your programming language.

Dharman
  • 30,962
  • 25
  • 85
  • 135
umka
  • 1,655
  • 1
  • 12
  • 18