help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: referring to the matched string in pattern replacement


From: Andy Chu
Subject: Re: referring to the matched string in pattern replacement
Date: Wed, 14 Apr 2021 09:51:45 -0700

I think you have to use a regex:

s=aXz
[[ $s =~ (.*)([A-Z])(.*) ]]
echo "${BASH_REMATCH[1]}(${BASH_REMATCH[2]})${BASH_REMATCH[3]}"
a(X)z

Andy




On Wed, Apr 14, 2021 at 6:59 AM Peng Yu <pengyu.ut@gmail.com> wrote:

> I want to replace a character matching a character class regex by a
> pair of brackets enclosing the matched strings.
>
> For example, if the regex is [A-Z], and the string is aXz, the output is
> a(X)z.
>
> I don't see this syntax is capable of doing this. I could spell out
> the characters in the character class, but that is tedious.
>
> ${parameter/pattern/string}
>
> Is there any other smart way to achieve this in bash efficiently
> without calling external programs?
>
> --
> Regards,
> Peng
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]