How can I use a look after to match either a single or a double quote?

前端 未结 3 595
天涯浪人
天涯浪人 2021-01-26 20:27

I have a series of strings I want to extract:

hello.this_is(\"bla bla bla\")
some random text
hello.this_is(\'hello hello\')
other stuff

What I

3条回答
  •  独厮守ぢ
    2021-01-26 20:44

    Use a capturing group and look for its content like the following:

    grep -Po 'hello\.this_is\(([\047"])((?!\1).|\\.)*\1\)' file
    

    This cares about escaped characters too e.g. hello.this_is("bla b\"la bla")

    See live demo here

    If the output should be what comes between parentheses then utilize both \K and a positive lookahead:

    grep -Po 'hello\.this_is\(([\047"])\K((?!\1).|\\.)*(?=\1\))' file
    

    Outputs:

    bla bla bla
    hello hello
    

提交回复
热议问题