'How to search for dialogues in quotation marks and dashes?

I would like to search for dialogues in between quotation marks and dashes, but I can't seem to code it.

Regarding dialogues marked by quotation marks, I have found a way to code it, mainly because that is the way dialogues are composed in English.

Here is my code and it has worked:

text = '''
Meneou a cabeça com ar triste e acrescentou:
- O homem acostuma-se a tudo, sim, a tudo, até a esquecer-se que é um homem...
- Ele disse que não sairia -

"Todos vamos ficando diferentes, e vinto e cinco anos é uma vida."

"Para muitos é mais do que isso."

"Claro que é"
'''

direto = re.findall(r'\"([^\"]+?)(\"|\-\-\n)',text)
for d in direto:
    print(d[0])

output:

"Todos vamos ficando diferentes, e vinto e cinco anos é uma vida.
Para muitos é mais do que isso.
Claro que é"

The output is precisely what I wanted. However, when it comes to spotting the dialogues between dashes, I can't seem to find the code. The desired output would have to show the dialogues that appear in between dashes and the ones followed by one dash.

The desired output:

"- O homem acostuma-se a tudo, sim, a tudo, até a esquecer-se que é um homem...
- Ele disse que não sairia -
Todos vamos ficando diferentes, e vinto e cinco anos é uma vida.
Para muitos é mais do que isso.
Claro que é"


Solution 1:[1]

  • To find text in quotation marks, use the pattern ".+". It allows all characters in between brackets as long as there is at least one character (that is the +).
  • To find text starting with a new line and a dash, and ending with a new line character, use: \s?-\s.+\s. This assumes any dashes in a text that is followed by a widespace (and optionalle precedet by one) to be a quote. You may let it end by a new line or a dash: (?=\n|(\s-)$). the $ indicates the end of the pattern and the ?= indicates that regex should look for the new line \n but not consume, i.e. print it.
  • To look for both patterns at the same run, use the OR condition: | (and you will neet to group expressions with brackets()
for d in re.finditer(r'(".+")|(-\s.+(?=\n|(\s-))$)', text, re.MULTILINE):
    print(d.group())

Yields the output

- O homem acostuma-se a tudo, sim, a tudo, até a esquecer-se que é um homem...
- Ele disse que não sairia -
"Todos vamos ficando diferentes, e vinto e cinco anos é uma vida."
"Para muitos é mais do que isso."
"Claro que é"

edit don't forget to set the re.MULTILINE flag (as I did oO) otherwise it will only look at the very beginning of the string and not of every line

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1