1
Fica outorgada, em nome da PREFEITURA MUNICIPAL DE
JUQUIÁ, CNPJ n. 46.585.964/0001-40, a autorização administrativa para interferência(s) em recursos hídricos superficiais, para
fins de rodoviário no município de JUQUIÁ, conforme abaixo
identificado:
- Travessia Aérea 01 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 54,00" - Longitude o 47° 38'
54,10" - Prazo 30 anos.
- Travessia Aérea 02 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 58,00" - Longitude o 47° 38'
52,20" - Prazo 30 anos.
- Travessia Aérea 03 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 58,00" - Longitude o 47° 38'
52,00" - Prazo 30 anos.
- Travessia Aérea 04 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 59,00" - Longitude o 47° 38'
51,70" - Prazo 30 anos.
- Travessia Aérea 05 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 59,40" - Longitude o 47° 38'
51,50" - Prazo 30 anos.
- Travessia Aérea 06 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 59,90" - Longitude o 47° 38'
51,30" - Prazo 30 anos. Processo DAEE 9502113 - Extrato de
Portaria 4063/19.


Second example:
Fica outorgada, em nome do DEPARTAMENTO DE ESTRADAS DE RODAGEM - DER, CNPJ n. 43.052.497/0002-85, a autorização administrativa para interferência(s) em recursos hídricos
superficiais, para fins de rodoviário no município de COTIA,
conforme abaixo identificado:
- Travessia Aérea 01 (TR-05)Estaca 248+11,50 BSTM de Ø
3,00 m - Córrego Foges (Córrego Caucaia) Km 50,491 - Coord.
Geográficas Latitude S 23° 38' 33,97" - Longitude o 47° 03'
28,67" - Prazo 30 anos.
- Travessia Aérea 02 (TR-06)Est. 275+13,70 3 Linhas de
BTTC de Ø 1,50 m
- Ribeirão da Vargem Km 51,124 - Coord. Geográficas
Latitude S 23° 38' 49,97" - Longitude o 47° 03' 32,20" - Prazo
30 anos.
- Travessia Aérea 03 (TR-07)Est. 290+11,15 BSTC de Ø
1,20 m - Afluente do Ribeirão da Vargem Km 51,420 - Coord.
Geográficas Latitude S 23° 38' 54,45" - Longitude o 47° 03'
41,39" - Prazo 30 anos.
- Travessia Aérea 04 (TR-08) Est. 307+11,00 2 Linhas de
BDTC de Ø 1,20 m
- Afluente do Ribeirão da Vargem Km 51,770 - Coord.
Geográficas Latitude S 23° 38' 57,81" - Longitude o 47° 03'
52,83" - Prazo 30 anos.
- Travessia Aérea 05 (TR-09) Est. 372+4,50 BSCC de 3,00 m
x 3,00 m - Afluente do Ribeirão da Vargem Km 53,020 - Coord.
Geográficas Latitude S 23° 39' 18,71" - Longitude o 47° 04'
34,24" - Prazo 30 anos. Processo DAEE 9822402 - Extrato de
Portaria 4068/19.

I'm trying to match the data from the beginning of each "topic". It should be:

(match1) - (match2) - Coord. (...... and the rest).

where match1 would be "Travessia Aérea 01" and match2 would be "Afluente do Rio Juquiá".

I'm trying to match with the expression:

- ? ?((([A-z-áàâãéèêíïóôõúç]|\d+)+ ? ? ?){1,10}) - ((([A-z-áàâãéèêíïóôõúç]|\d+)+ ? ? ?){1,10}) - Coord.

but It matches more then I wanted, because the matching doesn't stop with "- Coord.".

3 Answers3

0

I'm guessing that maybe this expression

^-\s*([^\r\n]*?)\s*-\s*([^\r\n]*?)\s*-\s*Coord\.

might be desired here.

Test

import re

expression = r"^-\s*([^\r\n]*?)\s*-\s*([^\r\n]*?)\s*-\s*Coord\."

string = """

Fica outorgada, em nome da PREFEITURA MUNICIPAL DE
JUQUIÁ, CNPJ n. 46.585.964/0001-40, a autorização administrativa para interferência(s) em recursos hídricos superficiais, para
fins de rodoviário no município de JUQUIÁ, conforme abaixo
identificado:
- Travessia Aérea 01 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 54,00" - Longitude o 47° 38'
54,10" - Prazo 30 anos.
- Travessia Aérea 02 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 58,00" - Longitude o 47° 38'
52,20" - Prazo 30 anos.
- Travessia Aérea 03 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 58,00" - Longitude o 47° 38'
52,00" - Prazo 30 anos.
- Travessia Aérea 04 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 59,00" - Longitude o 47° 38'
51,70" - Prazo 30 anos.
- Travessia Aérea 05 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 59,40" - Longitude o 47° 38'
51,50" - Prazo 30 anos.
- Travessia Aérea 06 - Afluente do Rio Juquiá - Coord.
Geográficas Latitude S 24° 19' 59,90" - Longitude o 47° 38'
51,30" - Prazo 30 anos. Processo DAEE 9502113 - Extrato de
Portaria 4063/19.

"""

print(dict(re.findall(expression, string, re.M)))
print(re.findall(expression, string, re.M))

Output

{'Travessia Aérea 01': 'Afluente do Rio Juquiá', 'Travessia Aérea 02': 'Afluente do Rio Juquiá', 'Travessia Aérea 03': 'Afluente do Rio Juquiá', 'Travessia Aérea 04': 'Afluente do Rio Juquiá', 'Travessia Aérea 05': 'Afluente do Rio Juquiá', 'Travessia Aérea 06': 'Afluente do Rio Juquiá'}


[('Travessia Aérea 01', 'Afluente do Rio Juquiá'), ('Travessia Aérea 02', 'Afluente do Rio Juquiá'), ('Travessia Aérea 03', 'Afluente do Rio Juquiá'), ('Travessia Aérea 04', 'Afluente do Rio Juquiá'), ('Travessia Aérea 05', 'Afluente do Rio Juquiá'), ('Travessia Aérea 06', 'Afluente do Rio Juquiá')]

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    Thanks Emma, this helped a lot. But if other string I still have a problem, because it's possible that the patterns I'm interested, have a - in the middle of letter, like this: Rio Mogi-Mirim. In this case, it wouldn't match correctly. I though if it was possible to use something like [^-], but to math ' - ', with spaces before and after the '-'. Do you have any idea how I could do that? I'll edit my post to put this other example.... thanks a lot! – Joao Vitor Maciel Sep 12 '19 at 18:17
0

Using re.split('\s*-\s+') to get the desired fields.

import re
with open('f0.txt', 'r', encoding='utf-8') as f:
    for line in f:
        if line.startswith('-'):
            _, Aérea, Afluente, _ = re.split(r'\s*-\s+', line)
            print(Aérea, Afluente)

Prints:

Travessia Aérea 01 Afluente do Rio Juquiá
Travessia Aérea 02 Afluente do Rio Juquiá
Travessia Aérea 03 Afluente do Rio Juquiá
Travessia Aérea 04 Afluente do Rio Juquiá
Travessia Aérea 05 Afluente do Rio Juquiá
Travessia Aérea 06 Afluente do Rio Juquiá
Chris Charley
  • 6,403
  • 2
  • 24
  • 26
0

The pattern that you tried has a lot op optional parts which makes it inefficient.

You could use 2 capturing groups an a negated character class matching not - or a newline without optional parts or non greedy quantifiers.

If the text is separated by a space hyphen space you could match those literally.

Note that \s also matches a newline and using A-z in a character class matches more than [A-Za-z].

^- ([^\r\n-]*) - ([^\r\n-]*) - Coord\.

Explanation

  • ^- Start of string, - and space
  • ([^\r\n-]*) Capture group 1 Match 0+ times not a newline or -
  • - Match literally
  • ([^\r\n-]*) Capture group 2 Match 0+ times not a newline or -
  • - Match literally
  • Coord\. Match Coord followed by literal dot

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70