如何引用在bash正则表达式替换捕获 [英] How to reference captures in bash regex replacement

查看:77
本文介绍了如何引用在bash正则表达式替换捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能包括在BASH更换前pression的正则表达式匹配?

非工作的例子:

 #!/斌/庆典
名称=约书亚
回声$ {//名[OA] / X \\ 1}

我期望的输出 jXoshuXa \\ 1 由匹配字符所取代。

这实际上并没有工作,虽然和输出 jX1shuX1 代替。


解决方案

 庆典>名称=约书亚
庆典>回声$名称| SED的/ \\([OA] \\)/ X \\ 1 / G'
jXoshuXa

How can I include the regex match in the replacement expression in BASH?

Non-working example:

#!/bin/bash
name=joshua
echo ${name//[oa]/X\1}

I expect to output jXoshuXa with \1 being replaced by the matched character.

This doesn't actually work though and outputs jX1shuX1 instead.

解决方案

bash> name=joshua  
bash> echo $name | sed 's/\([oa]\)/X\1/g'  
jXoshuXa

这篇关于如何引用在bash正则表达式替换捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆