Spaces around all hyphens in a string without double-up

Posted by Dave on Stack Overflow See other posts from Stack Overflow or by Dave
Published on 2013-10-22T03:36:35Z Indexed on 2013/10/22 3:54 UTC
Read the original article Hit count: 190

Filed under:

I'm after a regex that puts spaces around each "-" in a string, eg.

02 jaguar-leopard, tiger-panther 08

would become

02 jaguar - leopard, tiger - panther 08

Note that if the "-" already has spaces around it, no changes are to be made, eg.

02 jaguar - leopard, tiger - panther 08

should not become

02 jaguar  -  leopard, tiger  -  panther 08

The number of hyphens are unknown in advance.

Thanks for any ideas...

Edit: I'm not actually using a language for this. I'm using Ant Renamer (a mass file renaming utility). There are two fields in the renamer GUI, "Expression" and "New name" to provide inputs. This is from the help file as an example:

    Swapping artist and title from mp3 file names:

    "Expression" = (.*) - (.*)\.mp3 
    "New name" = $2 - $1.mp3 

    Extract episode number and title from series video files with episode number as SnnEmm     followed by title:

    "Expression" = Code\.Quantum\.S([0-9]{2})E([0-9]{2})\.(.*)\.FRENCH.XViD\.avi 
    "New name" = Code Quantum - $1$2 - $3.avi 

© Stack Overflow or respective owner

Related posts about regex