Regex to select only numbers from 1 to 2,000

1

I'm doing a search to return the article numbers, I used:

preg_match_all('/\d+/', $novalinhas, $matches); 
$artigo = (int)implode('',$matches[0]);  

It's working, however, in some articles it takes more numbers than the article, but it does not capture all the numbers in the string.

For example in this article below his id is article2040827.

Art. 2.040. The legal mortgage of the property of the tutor or curator, registered in accordance with item IV of art. 827 of the previous Civil Code, Law no. 3,071, of January 1, 1916, may be canceled, obeying the provisions of the sole paragraph of art. 1.745 of this Code.

I tried to use

[0-9] +. [0-9] + // Before art. 100, only article0 mark, and after 999, only mark article1

Note: Articles have variations of:

Art. 1º until Art. 2,023.

    
asked by anonymous 24.04.2015 / 15:51

2 answers

4

1 to 2023

\b(\d{1,3}|1\d{1,3}|20[01][0-9]|202[0-3])\b

link

1 to 2000

\b(\d{1,3}|1\d{1,3}|2000)\b

REGEX are used to match patterns, in the case of mine with combinations of numbers 1 - 2023. In other words, it is working. If you have a problem it is because you have not set your default, in case you just want the article there you have to see your text to try to extract a pattern.

One possible example would be

  

Art. 182

Trying to extract information: Does it always start with Art. Do you have any other variation? Is this sequence of characters or can it be any other? Can it happen to have more than one space it and the article number?

What is the size of the article number, can you have mile separator . as 2.212 will it be just the 2012 number or both?

Are all numbers followed by º ? Are there variations such as ª ?

A regex assumption that might meet is:

Art.\s+(\d{1,3}|1\d{1,3}|20[01][0-9]|202[0-3])º?

Since you would have the group with the numbers only, you can get this result in $ matches (from a var_dump ($ matches) to see the structure of the variable and how the results are)

  

Art. 1º until Art. There is no variation of th. It has the   miles. The º is only from 1 to 10, then the numbers end with a point and   space only.

Art\.\s+(1\.\d{3}|2\.0[0-3]\d|2\.04[0-6]|\d{1,3})º?\.?

link

    
24.04.2015 / 15:59
1

I was studying and I came up with a much simpler solution.

              $artigo =  explode(" ", $novalinhas);
              $caracteres = array("º",".");
              $artigos = str_replace($caracteres, "", $artigo[1]);
    
25.04.2015 / 07:11