регулярное выражение - цитаты внутри цитат - PullRequest
0 голосов
/ 10 февраля 2020

Я ищу регулярное выражение, которое может найти кавычки в стороне от кавычек. Я целый день тестировал разные варианты и нашел два близких:

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

Для этого регулярного выражения я вижу, что все работает, за исключением того, что кавычки внутри кавычек не найдены. (например: не соответствует "Come!", "test", "test1", ... из моего примера ниже.

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

Слишком сложный со многими fl aws. Я думаю, что там должно быть более простое решение.

Я использую образец из 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 голосов
/ 09 марта 2020

Предполагая, что вложенная кавычка может быть определена как кавычка с пробелом справа от нее («Привет здесь», - сказала она.)

Может работать следующая команда:

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

Что дает мне следующий вывод:

"Come!" 
"test" 
"test1" 
"test3" 
"Oh, no, not distressed me," 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...