Regex to select only numbers from 1 to 2,000

Asked

Viewed 612 times

1

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

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

It is working, however, in some articles it picks more numbers besides the article, but it does not capture all the numbers in the string.

For example in this article below his id is artigo2040827.

Art. 2.040. The legal mortgage of the property of the guardian or curator, registered in accordance with Article IV. 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 the art. 1.745 of this Code.

I tried to use

[0-9]+. [0-9]+ // Before art. 100, only Article0, and after 999, only Article1

Note: The articles have variations of:

Art. 1 to Art. 2,023.

  • 1 by 2000 or 1 by 2023?

  • There are several laws, so the number of articles varies. The largest I’ve seen so far is up to 2046 articles.

2 answers

4


from 1 to 2023

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

https://regex101.com/r/pK1jE5/1

from 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. That is to say it is working. If you have any problem is because you have not set your default, in case you just want the article ai have to see your text to try to extract a pattern.

A possible example would be

Art. 182º

Trying to extract the information: Always starts with Art. does it have any other variation? Is it this sequence of characters or could it be any other one? It can happen to have more than one space she and the article number?

What size item number, can have mile separator . as 2.212 will just be the number 2012 or both?

All numbers are followed by º? It has variations such as ª?

An assumption of regex that could meet is:

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

Since you would only have the group with the numbers being able to pick this result in $Matches (from a var_dump($Matches) to see the structure of the variable and how are the results)


Art. 1 to Art. 2,046. No variation of ª. Has the division of miles. º 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})º?\.?

https://regex101.com/r/pK1jE5/3

  • I actually have 2,300 articles. But in its line b( d{1,3}|1 d{1,3}|20[012][0-3]) b It skips selection in 2004 until 2009 and tb from 2014 until 2019 ".

  • vdd fixed the first

  • send an example of this article ai da para fazer mais testes

  • \b( d{1,3}|1 d{1,3}|20[01][0-9]|202[0-3]) b - still shows Art. 1º to 10º as Article 0.

  • only put º at the end in regex as optional

  • or take out the edges b .. b

  • Ohhhh! who knows, man. kkk. It worked, only has the problem that in some articles it takes apart from the article for example the article 57 Its id looks like this: artigo57111272005 Art. 57. The exclusion of the associate is only admissible if there is a just cause, thus recognized in a procedure that ensures the right of defence and of appeal, as provided in the statute. (Amended by Law 11.127, 2005) The test is on this page http://answall.com/questions/59853/divide-texto-em-ul-com-id-do-conte%C3%Bado-da-string

  • Art. 1º to Art. 2.046. Has no variation of ª. Has the division of miles. º is only 1 to 10, then numbers end with a dot and space only.

Show 3 more comments

1

I was studying and arrived at a much simpler solution.

              $artigo =  explode(" ", $novalinhas);
              $caracteres = array("º",".");
              $artigos = str_replace($caracteres, "", $artigo[1]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.