Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex: return characters inside parenthensis

I can't seem to get the values inside a parenthesis using grep.

echo "(this is a string)" | grep -Eo '[a-z ]*'

Ideally that should return the value inside the parenthesis, "this is a astring", instead it is not returning anything. Does anyone know the explanation?

like image 957
user2661842 Avatar asked May 11 '26 22:05

user2661842


1 Answers

This grep with -P (perl regex) works:

echo "foo (this is a string) bar" | grep -Po '\(\K[^)]*'
this is a string

OR using awk:

echo "foo (this is a string) bar" | awk -F '[()]+' '{print $2}'
this is a string

OR using sed:

echo "foo (this is a string) bar" | sed 's/^.*(\(.*\)*).*$/\1/'
this is a string
like image 56
anubhava Avatar answered May 13 '26 13:05

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!