Find and replace string with regex

Viewed 73

This may be a newbie regex question (still learning), but I'm trying to uncapitalize all the objects in my code (but not classes); i.e 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 (i.e Bishop), to be 'kept' (be it ; or .), which is what explains the bishop./g.

1 Answers

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()...
Related