PHP regular expression

橙三吉。 提交于 2020-01-07 07:35:52

问题


i have huge string that i need to separate information. Some parts of it vary and some dont. The difficulty i am facing is that i cant find a symbol or something on which i could get the match i want. So here is the string:

$str = "01;01;283;Póvoa do Vâle do Trigo;15315100 01;01;249;Alcafaz;;;;;;;;;;;3750;011;AGADÃO 01;01;2504;Caselho;;;;;;;;;;;3750;012;AGADÃO _ "15" '' ghdhghg AND IT CONTINUES

so if we look at the first part of the string (01;01;283;Póvoa do Vale do Trigo;15315100), what i want to stay with is:

01;01;283

and remove the rest of the stuff

in every case, but looking at the first example... : the 01 is always a number never superior to 2 (not 040 or 150505 or 4075) the same for the next 01 never superior to 2 (not 405 or 1565 or 425) then the 283 is the number that can be bigger, it varies (it can be 300 or 17581 or 40755794)

essentially in the end i want only the beginning of each part like:

01;01;283
01;01;249
01;01;2504
05,80,104258
94,76,56789124

sorry for any misspelling i am Portuguese


i forget to say that this separated parts will then go to an array! so the regular expression should not match for example like this:

15315100 01;01;249

so i cant use .+ for example

I AM USING PREG_REPLACE


回答1:


Try this:

/(\d+;\d+;\d+)/

Should work.




回答2:


Try the following. The regex is in the match_all line.

$str = "***01;01;283***;Póvoa do Vâle do Trigo;15315100 ***01;01;249***;Alcafaz;;;;;;;;;;;3750;011;AGADÃO ";
preg_match_all("/\*\*\*[01][0-9];[01][0-9];[0-9]*\*\*\*.*?/", $str, $matches);
print_r($matches);



回答3:


((?:\d\d;){2}\d+)

DEMO

And maybe it would be easier to just get everything between ***XXX***

\*([\d;]+)\*

DEMO



来源:https://stackoverflow.com/questions/19529922/php-regular-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!