regex - 字符串中所有连字符周围的空格没有加倍

标签 regex

我正在寻找一个正则表达式,它在字符串中的每个“-”周围放置空格,例如。

02 jaguar-leopard, tiger-panther 08

会变成

02 jaguar - leopard, tiger - panther 08

请注意,如果“-”周围已经有空格,则无需进行任何更改,例如。

02 jaguar - leopard, tiger - panther 08

不应该成为

02 jaguar  -  leopard, tiger  -  panther 08

连字符的数量事先未知。

感谢任何想法...

编辑: 我实际上并没有为此使用语言。我正在使用 Ant Renamer (大量文件重命名实用程序)。重命名器 GUI 中有两个字段,“表达式”和“新名称”用于提供输入。这是以帮助文件为例:

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

最佳答案

_*-_* 替换为 _-_(在使用模式之前将下划线替换为空格)。

这样,连字符周围的任何现有空格都将被新插入的空格替换。

关于regex - 字符串中所有连字符周围的空格没有加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508531/

相关文章:

javascript - 替换、正则表达式、javascript

JavaScript 正则表达式 - 替换时强制大小写

javascript - 正则表达式在 JavaScript 中无效

Javascript 只拆分一次并忽略其余部分

C++ regex_replace 清除连接的十进制数

javascript - JS/正则表达式 : Split string into words but using exception regex

regex - preg_match 允许字母和数字,但至少 1 个字母

正则表达式-匹配字符串时不带前导和尾随空格

python - 前瞻断言和使用 python 正则表达式分组

Python - 修改反向引用。可以吗?