简体   繁体   English

找出文件分隔符或正则表达式

[英]Find out file delimeter or regex

I receive a file everyday with some data:我每天都会收到一个包含一些数据的文件:

B024560HERR S, KARL-HEINZ          FAO031  D      F    18.07.17    01.08.17   14   LH 1162    18.07.17 10:30  12:35   FRA   FAO   FRA       1   LH 1163 01.08.17    FAO     03.01.17  1
    Frühbucher 10%
    inkl. Reiseleitung
    und Transfer ab/bis Flughafen
    *REPEATER, BITTE ZIMMERGEGENÜBER DER BAR 3ER
    UNVERBINDLICHER KUNDENWUNSCH
B024560HERR S, KARL-HEINZ          FAO031  D      F    18.07.17    01.08.17   14   LH 1162    18.07.17 10:30  12:35   FRA   FAO   FRA       1   LH 1163 01.08.17    FAO     03.01.17  1
B024560FRAU S, MARIA         FAO031  D      F    18.07.17    01.08.17   14   LH 1162    18.07.17 10:30  12:35   FRA   FAO   FRA       1   LH 1163 01.08.17    FAO     03.01.17  2

Link to this example file: https://www.dropbox.com/s/c39km8htjqq9q4t/example.txt?dl=1链接到此示例文件: https : //www.dropbox.com/s/c39km8htjqq9q4t/example.txt?dl=1

I need to import it to a mysql database.我需要将其导入 mysql 数据库。

So this should be divided like this (using | as a divider):所以这应该像这样划分(使用 | 作为分隔符):

B024560 | HERR S, KARL-HEINZ    |       FAO031  | D    |   F    | 18.07.17    |  01.08.17   | 14   | LH 1162   |  18.07.17 | 10:30  | 12:35   | FRA   | FAO   | FRA      |  1    |LH 1163  | 01.08.17   |  FAO   |   03.01.17  | 1  |
    Frühbucher 10%
    inkl. Reiseleitung
    und Transfer ab/bis Flughafen
    *REPEATER, BITTE ZIMMERGEGENÜBER DER BAR 3ER
    UNVERBINDLICHER KUNDENWUNSCH  |

B024560  | HERR S, KARL-HEINZ       |    FAO031  | D     |  F    | 18.07.17  |   01.08.17  |  14   | LH 1162   |  18.07.17  | 10:30   |12:35  |  FRA  |  FAO  |  FRA    |    1  |  LH 1163 | 01.08.17  |   FAO  |    03.01.17  | 1  |

B024560  | FRAU S, MARIA  |        FAO031  |  D   |    F   |  18.07.17   |  01.08.17   | 14   |  LH 1162  |   18.07.17  | 10:30   | 12:35  |   FRA  |  FAO   | FRA   |     1  |  LH 1163  | 01.08.17   |  FAO    |  03.01.17  | 2

The best that I have done was with this regex :我所做的最好的是使用这个正则表达式

\s{2,}

So my problem is how can I find out the file delimeter ?所以我的问题是如何找出文件分隔符? Or is there another or better way to do this?或者有另一种或更好的方法来做到这一点? The idea is to read this file with php and import to mysql database.这个想法是用php读取这个文件并导入到mysql数据库中。

This will do what you want:这将执行您想要的操作:

(?<=\w)\h{2,}(?=\w)        # horizontal spaces with word character beneath
|
(?=HERR|FRAU)              # "HERR" or "FRAU" literally
|
\h+(?=\d{2}\.\d{2}\.\d{2}) # a date
|
\h+(?=\d{2}:\d{2})         # time of date in xx:xx format

See your modified demo on regex101.com (and mind the modifiers!).在 regex101.com 上查看您修改后的演示(并注意修饰符!)。


The whole snippet: 整个片段:

 <?php $text = <<<EOT B024560HERR S, KARL-HEINZ FAO031 DF 18.07.17 01.08.17 14 LH 1162 18.07.17 10:30 12:35 FRA FAO FRA 1 LH 1163 01.08.17 FAO 03.01.17 1 Frühbucher 10% inkl. Reiseleitung und Transfer ab/bis Flughafen *REPEATER, BITTE ZIMMERGEGENÜBER DER BAR 3ER UNVERBINDLICHER KUNDENWUNSCH B024560HERR S, KARL-HEINZ FAO031 DF 18.07.17 01.08.17 14 LH 1162 18.07.17 10:30 12:35 FRA FAO FRA 1 LH 1163 01.08.17 FAO 03.01.17 1 B024560FRAU S, MARIA FAO031 DF 18.07.17 01.08.17 14 LH 1162 18.07.17 10:30 12:35 FRA FAO FRA 1 LH 1163 01.08.17 FAO 03.01.17 2 EOT; $regex = '~ (?<=\\w)\\h{2,}(?=\\w) | (?=HERR|FRAU) | \\h+(?=\\d{2}\\.\\d{2}\\.\\d{2}) | \\h+(?=\\d{2}:\\d{2}) ~x'; $lines = explode("\\n", $text); foreach ($lines as $line) { $fields = preg_split($regex, $line); if (count($fields) > 1) { print_r($fields); } } ?>

See it working on ideone.com .在 ideone.com 上查看它。

Kind of simple way.一种简单的方法。

Use (?m)^(B\\d+)((?:(?![ ]{9}).)+)[ ]+(.+) and a callback function.使用(?m)^(B\\d+)((?:(?![ ]{9}).)+)[ ]+(.+)和回调函数。

Pass $1, $2, $3 to callback.将 $1, $2, $3 传递给回调。

Split $3 with \\s+.用 \\s+ 分割 $3。 $result = Join split array with |. $result = 用 | 连接拆分数组。
Return = Join $1 |返回 = 加入 $1 | $2 | $2 | + $result + $结果

 (?m)
 ^
 ( B \d+ )                     # (1)
 (                             # (2 start)
      (?:
           (?! [ ]{9} )
           .
      )+
 )                             # (2 end)
 [ ]+ 
 ( .+ )                        # (3)

Match example output匹配示例输出

 **  Grp 1 -  ( pos 0 , len 7 ) 
B024560  
 **  Grp 2 -  ( pos 7 , len 18 ) 
HERR S, KARL-HEINZ  
 **  Grp 3 -  ( pos 35 , len 148 ) 
FAO031  D      F    18.07.17    01.08.17   14   LH 1162    18.07.17 10:30  12:35   FRA   FAO   FRA       1   LH 1163 01.08.17    FAO     03.01.17  1  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM