Replace patterns that are inside delimiters using a regular expression call

后端 未结 5 1065
轮回少年
轮回少年 2021-01-07 02:13

I need to clip out all the occurances of the pattern \'--\' that are inside single quotes in long string (leaving intact the ones that are outside single quotes). <

5条回答
  •  轮回少年
    2021-01-07 02:38

    You can use the following sed script, I believe:

    :again
    s/'\(.*\)--\(.*\)'/'\1\2'/g
    t again
    

    Store that in a file (rmdashdash.sed) and do whatever exec magic in your scripting language allows you to do the following shell equivalent:

    sed -f rmdotdot.sed < file containing your input data

    What the script does is:

    :again <-- just a label

    s/'\(.*\)--\(.*\)'/'\1\2'/g

    substitute, for the pattern ' followed by anything followed by -- followed by anything followed by ', just the two anythings within quotes.

    t again <-- feed the resulting string back into sed again.

    Note that this script will convert '----' into '', since it is a sequence of two --'s within quotes. However, '---' will be converted into '-'.

    Ain't no school like old school.

提交回复
热议问题