php - 多行 php 上数字的正则表达式

标签 php regex

我有一个看起来像这样的文件(是的,换行符是正确的):

39                                              9
30 30 30 31 34 30 30 32 33 32 36 30 31 38 0D 0A 00014002326018..
39 30 30 30 31 34 30 30 32 33 32 36 30 35 34 0D 900014002326054.
0A                                              .
39 30 30 30 31 34 30 30 32 33 32 36 30 39 31 0D 900014002326091.
0A                                              .
39 30 30 30 31 34 30 30 32 33 32 36 31 36 33 0D 900014002326163.
0A                                              .
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 32 30 30 0D 0A                            26200..
39                                              9
30 30 30 31 34 30 30 32 33 32 36 32 30 30 0D 0A 00014002326200..
39 30 30 30 31 34 30 30 32 33 32 36 31 32 32 0D 900014002326122.
0A                                              .
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 31 35 34 0D 0A                            26154..
39 30 30 30 31 34 30 30 32 33                   9000140023
32 36 31 33 31 0D 0A                            26131..
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 31 30 34 0D 0A                            26104..
39 30 30 30 31 34 30 30 32 33 32 36 30 39 30 0D 900014002326090.
0A                                              .
39 30 30 30 31 34 30 30 32 33 32 36 31 39 37 0D 900014002326197.
0A                                              .
39                                              9
30 30 30 31 34 30 30 32 33 32 36 32 30 38 0D 0A 00014002326208..
39 30 30 30 31 34 30 30 32 33                   9000140023
32 36 31 31 35 0D 0A                            26115..
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 31 36 34 0D 0A                            26164..
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 30 31 36 0D 0A 39 30 30 30 31 34 30 30 32 26016..900014002
33                                              3
32 36 32 34 36 0D 0A                            26246..
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 32 34 36 0D 0A                            26246..
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 30 37 39 0D 0A                            26079..
39                                              9
30 30 30 31 34 30 30 32 33                      000140023
32 36 31 32 30 0D 0A                            26120..
39                                              9
30 30 30 31 34 30 30 32 33 32 36 32 32 38 0D 0A 00014002326228..
39 30 30 30 31 34 30 30 32 33                   9000140023
32 36 31 38 36 0D 0A                            26186..

我有这段代码可以获取 EID 标签(以 9000 开头的数字),但我不知道如何让它执行多行。

$data = file_get_contents('tags.txt');

$pattern = "/(\d{15})/i";

preg_match_all($pattern, $data, $tags);
$count = 0;
foreach ( $tags[0] as $tag ){

    echo $tag . '<br />';
    $count++;
}

echo "<br />" . $count . " total head scanned";

例如第一行和第二行应该返回900014002326018而不是忽略第一行和第二行

我不擅长正则表达式,所以如果你能解释一下,这样我就可以学习并不再需要有人帮助我使用简单的正则表达式,那就太棒了。

编辑:整数是从 9000 开始的 15 位数字

最佳答案

你可以这样做:

$result = preg_replace('~\R?(?:[0-9A-F]{2}\h+)+~', '', $data);
$result = explode('..', rtrim($result, '.'));

图案细节:

\R?            # optional newline character
(?:            # open a non-capturing group
  [0-9A-F]{2}  # two hexadecimal characters
  \h+          # horizontal white characters (spaces or tabs)
)+             # repeat the non-capturing group one or more times

在此替换之后,您唯一必须删除的内容是两个点。删除尾随点后,您可以使用它们将字符串分解为数组。

另一种方式

因为您知道整数(和点)部分之前总是有 48 个字符,所以您也可以使用这种模式:

$result = preg_replace('~(?:^|\R).{48}~', '', $data);

另一种没有正则表达式的方式

想法是逐行读取文件,由于内容之前的长度始终相同(即 16*3 个字符 -> 48 个字符),提取带有整数的子字符串并将其连接到 $data 临时变量。

ini_set("auto_detect_line_endings", true);
$data = '';
$handle = @fopen("tags.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 128)) !== false) {
        $data .= substr($buffer, 48, -1);
    }
    if (!feof($handle)) {
        echo "Error: fgets() has failed\n";
    }
    fclose($handle);
} else {
    echo "Error opening the file\n";
}

$result = explode ('..', rtrim($data, '.'));

注意:如果文件是 windows 格式(以 \r\n 行结尾),您必须将 substr() 函数的第三个参数更改为-2。如果您对如何检测换行类型感兴趣,可以查看this post。 .

关于php - 多行 php 上数字的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20292178/

相关文章:

javascript - 在Electron中,如何使用 Electron 打包器仅包含特定的node_modules?

javascript - 我们可以用字符串变量检查 match() 函数吗

Python正则表达式删除字符串中的url和域名

html - 需要一个正则表达式来匹配多个类之一

PHP,用一个带字母的数组按字母顺序排序

php - Laravel 5.3 形式的 TokenMismatchException

php - Phalcon 关系顺序

php - 发布到受密码保护的 URL?

php - 将mysql表导出到xml

c# - 正则表达式搜索和替换,其中替换是搜索词的模组