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

家住魔仙堡 提交于 2019-12-11 08:35:36

问题


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 need to get (from many files, but this is not important here) is the content between hello.this_is( and ), so my desired output is:

bla bla bla
hello hello

As you see, the text within parentheses can be enclosed with either double or single quotes.

If this was only single quotes I would use a look behind and look ahead just like this:

grep -Po "(?<=hello.this_is\(').*(?=')" file
#                            ^      ^
# returns ---> hello hello

Similarly, to get strings from double quotes I would say:

grep -Po '(?<=hello.this_is\(").*(?=")' file
#                            ^      ^
# returns ---> bla bla bla

However, I want to match both cases, so it gets both single and double quotes. I tried with using $'' to escape, but could not make it work:

grep -Po '(?<=hello.this_is\($'["\']').*(?=$'["\']')' file
#                            ^^^^^^^^      ^^^^^^^^

I can of course use the ASCII number and say:

grep -Po '(?<=hello.this_is\([\047\042]).*' file

but I would like to use the quotes and single quotes, since 047 and 042 are not that much representative to me as single and double quotes are.


回答1:


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 ^



回答2:


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



回答3:


Based on revo and hek2mgl excellent answers, I ended up using grep like this:

grep -Po '(?<=hello\.this_is\((["'\''])).*(?=\1)' file

Which can be explained as:

  • grep
  • -Po use Perl regexp machine and just prints the matches
  • '(?<=hello\.this_is\((["'\''])).*(?=\1)' the expression
    • (?<=hello\.this_is\((["'\''])) look-behind: search strings preceeded by "hello.this_is(" followed by either ' or ". Also, capture this last character to be used later on.
    • .* match everything...
    • (?=\1) until the captured character (that is, either ' or ") appears again.

The key here was to use ["'\''] to indicate either ' or ". By doing '\'' we are closing the enclosing expression, populating with a literal ' (that we have to escape) and opening the enclosing expression again.



来源:https://stackoverflow.com/questions/55504747/how-can-i-use-a-look-after-to-match-either-a-single-or-a-double-quote

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!