正则表达式-引号内的引号

时间:2020-02-10 02:35:15

标签: regex perl awk sed grep

我正在寻找一个可以在引号的一边找到引号的正则表达式。我整天都在测试不同的选项,发现其中两个很接近:

("([^"]|"")*")

对于此正则表达式,我看到一切正常,除了找不到引号内的引号。 (例如:与以下示例中的“来吧!”,“测试”,“测试1” ...不匹配。 和

(\s|^)\"[[:graph:]].[^$]*?((\".[^$]*?\")*?|.[^\"])*\"\s

太复杂,有很多缺陷。我认为必须有一个更简单的解决方案。

我正在使用Dracula的示例来测试我的解决方案(可从Gutenburg项目获得)(注意:我添加了一些测试引号):

"Then he spoke to me mockingly, 'And so you, like the others, would play
your brains against mine. You would help these men to hunt me and
frustrate me in my designs! You know now, and they know in part already,
and will know in full before long, what it is to cross my path. They
should have kept their energies for use closer to home. Whilst they
played wits against me--against me who commanded nations, and intrigued
for them, and fought for them, hundreds of years before they were
born--I was countermining them. And you, their best beloved one, are now
to me, flesh of my flesh; blood of my blood; kin of my kin; my bountiful
wine-press for a while; and shall be later on my companion and my
helper. You shall be avenged in turn; for not one of them but shall
minister to your needs. But as yet you are to be punished for what you
have done. You have aided in thwarting me; now you shall come to my
call. When my brain says "Come!" to you, you shall cross land or sea to
do my bidding; and to that end this!' With that he pulled open his
shirt, and with his long sharp nails opened a vein in his breast. When
the blood began to spurt out, he took my hands in "test" one of his, holding
them tight, and with the other, my neck and pressed my mouth to
the wound, so that I must either suffocate or swallow some of the---- Oh
my God! my God! what have I done? What have I done to deserve such a
fate, I who have tried to walk in "test1" "test3" meekness and righteousness all my
days. God pity me! Look down on a poor soul in worse than mortal peril;
and in mercy pity those to whom she is dear!" Then she began to rub her
lips as though to cleanse them from pollution.


"Oh, no, not distressed me," she replied, "but I have been more touched
than I can say by your grief. That is a wonderful machine, but it is
cruelly true. It told me, in its very tones, the anguish of your heart.
It was like a soul crying out to Almighty God. No one must hear them
spoken ever again! See, I have tried to be useful. I have copied out the
words on my typewriter, and none other need now hear your heart beat, as
I did." 

我希望输出为:

"Then he spoke to me mockingly, 'And so you, like the others, would play
your brains against mine. You would help these men to hunt me and
frustrate me in my designs! You know now, and they know in part already,
and will know in full before long, what it is to cross my path. They
should have kept their energies for use closer to home. Whilst they
played wits against me--against me who commanded nations, and intrigued
for them, and fought for them, hundreds of years before they were
born--I was countermining them. And you, their best beloved one, are now
to me, flesh of my flesh; blood of my blood; kin of my kin; my bountiful
wine-press for a while; and shall be later on my companion and my
helper. You shall be avenged in turn; for not one of them but shall
minister to your needs. But as yet you are to be punished for what you
have done. You have aided in thwarting me; now you shall come to my
call. When my brain says "Come!" to you, you shall cross land or sea to
do my bidding; and to that end this!' With that he pulled open his
shirt, and with his long sharp nails opened a vein in his breast. When
the blood began to spurt out, he took my hands in "test" one of his, holding
them tight, and with the other, my neck and pressed my mouth to
the wound, so that I must either suffocate or swallow some of the---- Oh
my God! my God! what have I done? What have I done to deserve such a
fate, I who have tried to walk in "test1" "test3" meekness and righteousness all my
days. God pity me! Look down on a poor soul in worse than mortal peril;
and in mercy pity those to whom she is dear!"
"Come!"
"test"
"test1"
"test3"
"Oh, no, not distressed me,"
"but I have been more touched
than I can say by your grief. That is a wonderful machine, but it is
cruelly true. It told me, in its very tones, the anguish of your heart.
It was like a soul crying out to Almighty God. No one must hear them
spoken ever again! See, I have tried to be useful. I have copied out the
words on my typewriter, and none other need now hear your heart beat, as
I did." 

对于正则表达式,我正在使用grep -P(Perl),但也可以使用awk或sed。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

假设可以将嵌套引号确定为引号后方有一个空格(她说:“ ​​Hello here”。)

以下命令可能有效:

cat test.txt | sed 's/"[^"]*" /\n&\n/g' | grep '".*"'

哪个给我以下输出:

"Come!" 
"test" 
"test1" 
"test3" 
"Oh, no, not distressed me,"