简体   繁体   中英

Find and replace string with regex

This may be a newbie regex question (still learning), but I'm trying to uncapitalize all the objects in my code (but not classes); ie for the code snippet:

Bishop_Piece Bishop;

Bishop.move()...

I want it to instead be:

Bishop_Piece bishop;

bishop.move()...

What I tried:

find . -type f | xargs sed -i  's/Bishop[;.]/bishop./g'

However this results in:

Bishop_Piece bishop.

bishop.move()...

Basically, I want the character after what I'm searching for (ie Bishop), to be 'kept' (be it ; or . ), which is what explains the bishop./g .

You can use

find . -type f | xargs sed -i 's/Bishop\([;.]\)/bishop\1/g'

Note that the \([;.]\) here defines a capturing group whose value is referred to with \1 from the replacement pattern.

The (...) parentheses are escaped since this is a POSIX BRE compliant regular expression.

See the online demo :

s='Bishop_Piece bishop;
bishop.move()...'
sed 's/Bishop\([;.]\)/bishop\1/g' <<< "$s"

Output:

Bishop_Piece bishop;
bishop.move()...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM