A regular expression to detect acronyms of Brazilian highways

1

I'm trying to detect if a particular address corresponds to a Brazilian highway.

For example, br-101 matches.

My initial plan was to list the state acronyms (mg, sp, rn ...) plus the abbreviation br, and write something like /sigla1-[0-9]{3}|sigla2-[0-9]{3}.../ .

But a wikipedia query gave me a surprise: there are other prefixes besides states. (for example, prc , in link )

I ask, then, which is the most correct way to detect the highways?

We can get (duas_ou_tres_letras)-(tres_numeros) , for example. Does the part before the hyphen have necessarily two or three letters? the part after the hyphen can have less than three numbers?

Would anyone have a list of possible acronyms that might come before the hyphen?

    
asked by anonymous 07.08.2017 / 23:57

2 answers

2

I found the question interesting and tried to inform me how the nomenclature of the highways of Brazil works.

According to the federal government highways there is a standard for definition of the names of federal highways. And by the I researched I could see that this pattern is also adopted on state highways, but there are exceptions.

The first number of the name of the highway, for example, BR- 3 07 has meaning and varies from 0 to 6. And state highways also apply.

  • Radial highways: BR-0xx - highways departing from the federal capital toward the extreme of the country
  • Longitudinal Roads: BR-1xx - Highways that cut the country in north-south direction
  • Transverse highways: BR-2xx - highways that cut the country in the direction east-west
  • Diagonal highways: BR-3xx - highways can present two modes of orientation: northwest-southeast or northeast-southwest
  • Connecting highways: BR-4xx - highways present in any direction. There are also highways starting with BR-6xx, but there are few of short extension.

It would be interesting to confirm this information to make the regex more accurate, eg:

  • We know that the first information is uppercase and varies of 2 to 3 letters: [A-Z]{2,3}
  • There is a hyphen between letters and numbers: -
  • The first number ranges from 0 to 6:% with%
  • E ends with two more digits: [0-6]

Finally your regex would look like this: [0-9]{2} . Functional sample

    
08.08.2017 / 14:02
1

You can mount two more generic regex to validate just the format of the highway and another more specialized one that guarantees with greater chances its existence.

By the research I did some highways receives a C after the state acronym as they are coincident ie a stretch of a federal highway is in the same stretch of a state and it is the responsibility of the state to maintain conservation but found no list centralized each is keeps its own list.

Not all states have concurrent highway as soon as the second regex home invalid values like BRC-000 or ACC-00 so an additional treatment is required in the application as an exceptions list or find out which states have those highways and refine more regex. / p>

The generic would be:

[A-Z]{2,3}-[0-9]{3}

Entries:

BR-101 //OK
ABC-100 //OK
ZZ-000 //OK

Example - regex101

The other would be the list of acronyms of states followed by an optional C fault followed by dash and three numbers.

(AC|AL|AP|AM|BA|CE|DF|ES|GO|MA|MT|MS|MG|PA|PB|PR|PE|PI|RJ|RN|RS|RO|RR|SC|SP|SE|TO|BR)C?-[0-9]{3}

Entries:

BR-101 //OK
ABC-000 //fora do padrão
ZZZ-999 //fora do padrão
PRC-280 //OK
RSC-453 //OK
BRC-000 //OK mas é inválida
ACC-999 //OK mas é inválida

Example - regex101

    
08.08.2017 / 00:34