正则表达式 - 如何匹配空格并用冒号替换它

时间:2015-01-20 16:50:28

标签: regex space

文字看起来像这样:

816 Organizational Relationships
832 Price/Sales Catalog
846 Inventory Inquiry/Advice
850 Purchase Order
855 Purchase Order Acknowledgment

我需要用,

替换第一个空格

谢谢

1 个答案:

答案 0 :(得分:0)

用逗号替换任何第一个类似空格的字符(例如制表符或空格)(如果您确实需要冒号,请将,替换为:

/\s/,/

(注意缺少g标志)

示例Bash运行:

$> echo '816 Organizational Relationships
> 832 Price/Sales Catalog
> 846 Inventory Inquiry/Advice
> 850 Purchase Order
> 855 Purchase Order Acknowledgment' | sed -e 's/\s/,/'

816,Organizational Relationships
832,Price/Sales Catalog
846,Inventory Inquiry/Advice
850,Purchase Order
855,Purchase Order Acknowledgment
$>