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

前端 未结 3 566
天涯浪人
天涯浪人 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:40

    Note: The sed command at the bottom of this answer works only as long as your strings are nice behaving strings like

    "foo"
    

    or

    'bar'
    

    As soon as your strings start to misbehave :) like:

    "hello \"world\""
    

    it won't work any more.

    Your input looks like source code. For a stable solution I recommend to use a parser for that language to extract the strings.


    For trivial use cases:

    You can use sed. The solution is supposed to work on any POSIX platform in contrast to grep -oP which only works with GNU grep:

    sed -n 's/hello\.this_is(\(["'\'']\)\([^"]*\)\(["'\'']\).*/\2/gp' file
    #                                    ^^^^^^^^              ^^
    #                                          capture group 2 ^
    

提交回复
热议问题