adamsdesk, (edited )
@adamsdesk@fosstodon.org avatar

I must have my thoughts crossed as for the life of me I can't seem to figure out this regex in BASH using a parameter expansion.

I want to search for 2 or more dashes and then replace with one dash. Here is what I thought would work, but doesn't. Any suggestions?

string="Hello-World! command---line"
string="${title//[-]{2,}/-}"

techaddressed,
adamsdesk,
@adamsdesk@fosstodon.org avatar

@techaddressed 😆 and a Star Trek reference 😁.

techaddressed,

@adamsdesk I had seen this a long while ago and created this better looking version to use in a blog post lol

adamsdesk,
@adamsdesk@fosstodon.org avatar

@techaddressed It's rare for me to find a meme funny, but you got me.

adamsdesk,
@adamsdesk@fosstodon.org avatar

@techaddressed The house has honor again. 🙂

techaddressed,

@adamsdesk Qapla'!

adamsdesk,
@adamsdesk@fosstodon.org avatar

@techaddressed Qapla'!

hyde,
@hyde@lazybear.social avatar

@adamsdesk Just use :

:s'//'-'g

adamsdesk,
@adamsdesk@fosstodon.org avatar

@hyde Thank you, but this is a script that assists creating a new blog posts. It seems to be working well now.

hjr265, (edited )

@adamsdesk I don't think the pattern you pass there can be a regular expression. Bash has its own pattern matching rules.

https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html

I think this does what you are trying to do (replace 2 or more consecutive dashes with a single dash):

string="Hello-World! #debug command---line"  
string="${string//-+(-)/-}"  

But this will work only if you enable extglob first:

shopt -s extglob  

Alternatively, use sed:

string=`echo "$string" | sed -e 's/--\+/-/g'`  
adamsdesk,
@adamsdesk@fosstodon.org avatar

@hjr265 Oh really, I thought one could. That would explain why it's not working as desired.

I needed a different perspective. Thanks for a working solution.

GreatBigTable,
@GreatBigTable@mastodon.social avatar

@adamsdesk what does it output?

adamsdesk,
@adamsdesk@fosstodon.org avatar

@GreatBigTable Here is the output...

Hello-World! command---line/-

  • All
  • Subscribed
  • Moderated
  • Favorites
  • Bash
  • DreamBathrooms
  • ngwrru68w68
  • modclub
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • InstantRegret
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • GTA5RPClips
  • JUstTest
  • tacticalgear
  • normalnudes
  • tester
  • osvaldo12
  • everett
  • cubers
  • ethstaker
  • anitta
  • provamag3
  • Leos
  • cisconetworking
  • megavids
  • lostlight
  • All magazines