Reputation: 301
I have a working regex to extract several information. The php code is the folowing:
<?php
$re = "/(\\d{2}.\\d{2}.\\d{2}).+(\\w{3}).+\\w{3}.+(\\w{2}\\s\\d{4}).+(\\d{2}:\\d{2}\\n).+(\\d{2}.\\d{2}.\\d{2}).+(\\w{2}\\s\\d{4}).+(\\d{2}:\\d{2}\\n).+((FNC|PXO)\\d{3})/";
$str = "***NEUBUCHUNG ***\n 24.01.15 TXL FNC AB 2306 11:40 15:20\n 31.01.15 FNC TXL AB 2307 16:05\n FNC044 RESIDENCIAL VILA LUSITANI 9000-120 FUNCHAL\n 1 DOPPELZIMMER FRUEHSTUECK\n SPO1101\n INKL. REISELEITUNG UND TRANSFER AB/BIS\n FLUGHAFEN\n F368966 HERR EIDAM, KLAUS 54\n F368966 FRAU EIDAM, SONJA 54";
$str2 = "***ÄNDERUNG ***\nNEU:11.04.15 DUS AB 2646 13:15 16:25\n 18.04.15 FNC DUS AB 2647 17:15\n FNC027 PESTANA CARLTON MADEIRA 9004-531 FUNCHAL\n 1 DO-MEERBLICK F\nF365474 HERR PETERS, HANS O 03.01.15\nLANGZEITERMÄSSIGUNG 10%\nSPO-JAN_SALES 20%\nFRÜHBUCHER 10%\nINKL. REISELEITUNG UND TRANSFER AB/BIS\nFLUGHAFEN\nZimmer in ruhiger Lage\n(unverbindlicher Kundenwunsch)\nNEU:\nF365474 FRAU PETERS, ULRIKE O 03.01.15";
preg_match($re, $str, $matches);
print_r($matches)
?>
Regex with str: https://regex101.com/r/rF0uP7/5
Regex with str2: https://regex101.com/r/cV6iF9/1
However it works perfectly for str it does not match in str2, and I cant find the reason why
Upvotes: 1
Views: 54
Reputation: 7617
However it works perfectly for str it does not match in str2, and I cant find the reason why
Here is the Culprit Expression: (\\w{3}).+\\w{3}
And in $str you had 24.01.15 TXL FNC AB
But in $str2, you had: 11.04.15 DUS AB
Your Regex could read better like so:
$re = "#(\d{2}\.\d{2}\.\d{2})(?:\s+(\w{3}))?\s+\w{3}\s+(\w{2}\s\d{4}).+(\d{2}:\d{2}\n)\s+(\d{2}\.\d{2}\.\d{2}).+(\w{2}\s\d{4})\s+(\d{2}:\d{2}\n).+((FNC|PXO)\d{3})#si";
Upvotes: 2
Reputation: 626738
The .+(\w{3})
in the beginning must be optional. Wrap it with (?:.+(\w{3}))?
.
See the regex demo
Also, you have too many .+
, in most places, you meant to just match whitespaces, and thus are better turned into \s+
. Also, dots that are meant to match literal dots must be escaped.
Use a more optimized:
(\d{2}\.\d{2}\.\d{2})(?:\s+(\w{3}))?\s+\w{3}\s+(\w{2}\s\d{4}).+(\d{2}:\d{2}\n)\s+(\d{2}\.\d{2}\.\d{2}).+(\w{2}\s\d{4})\s+(\d{2}:\d{2}\n).+((FNC|PXO)\d{3})
See this regex demo
Upvotes: 1