RegEx parsing eshop params with PHP
sorry for my bad english. I have some params of ware in eshop like:
Mraznička
* Počet zásuvek mrazničky 3
* XXL zásuvka
* Mrazící výkon 4,5 kg/24 h
Rozměry balení:
Hmotnost (kg): 61.000
Výška (cm): 182.00
Šířka (cm): 64.00
Hloubka (c开发者_C百科m): 71.00
Typ: volně stojící
Konstrukce chladničky: kombinovaná
Umístění mrazícího prostoru: mraznička dole
Změna otevírání dveří: ANO
Ovládání: mechanické-knoflíkové
Displej: bez displeje
Energetická třída: A++
There are three kind of block and I need to choose, which one is.
Conditions for types:
1) Text block begin with any letter, but NOT with * and NOT ending with :, this line must be followed by new line(s) beg. with *
2) Text block begin with any letter, but not with * and ending with :, this line must be followed by new line(s) NOT beg. with *
3) Line(or lines) begin with word(od word), then following char ":" and then following any othes word(or words)
Can you help me, how can I identify type of textblock? I need to check each textblock separately - parsing long text to block is allready done and works fine.
Thanks.
Added a possible solution for the 3 cases with a link to a online regex tester tool.
Each of these regex will only match one case of the block types.
As a precondition I assumed that the blocks are always separated by empty lines.
Edit
Minor updated regex inspired by comment (that was posted as separate answer) case 2 and 3 can overlap thus the regex now force empty line before each block.
1) http://www.myregextester.com/?r=df2be635
^[\r\n]{1,2}(?:[^*].+[^:][\r\n]{1,2})(?:\*.+[\r\n]{1,2})+$
2) http://www.myregextester.com/?r=f903ae6d
^[\r\n]{1,2}(?:[^*].+:[\r\n]{1,2})(?:[^*\s].+[\r\n]{1,2})+$
3) http://www.myregextester.com/?r=17ed0af8
^[\r\n]{1,2}(?:[^*].+:.+[^:][\r\n]{1,2})(?:[^*\s].+[\r\n]{1,2})+$
For all three cases the result will be captured in matcher group [0]. The regex is composed of two non capturing groups for the first line and the following repeated list.
精彩评论