php - 坚持使用 PHP 和 Regex 为 MYSQL 准备 DHCP 输出
问题描述
嗨,我一直试图不使用 preg_replace 和 preg_match 替换某些元素,基本上我正在转换 dhcp 租约输出,以便我可以将元素插入数据库。
我可以在 notepad++ 中做我需要的事情,但我想使用 php 来自动化它,这是我在 notepad++ 中的正则表达式步骤:
第 1 步)代码
\s?-(\w)-\s?
\1~
步骤 2) 租赁
-\s?(\d\d/\d\d/\d\d\d\d\s\d\d:\d\d:\d\d|NEVER EXPIRES|INACTIVE)\s?
~\1~
步骤 3) 识别 IP
(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+?-\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})
\1~\2
第 4 步)识别 mac
\s?-\s?(([0-9A-Fa-f]{2}[:-]){3,6}([0-9A-Fa-f]{2}))
~\1
所需的输出将是由波浪号分隔的每个元素〜准备分解成一个我可以在插入语句中使用的数组:
192.25.200.4 ~ 255.255.255.192~ 64-9e-f3-b3-81-d3 ~01/08/2018 17:07:46 ~D~ BH1-WAP-01.local
下面是一些测试数据:
Changed the current scope context to 192.25.200.0 scope.
Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
============================================================================================
IP Address - Subnet Mask - Unique ID - Lease Expires -Type -Name
============================================================================================
192.25.200.4 - 255.255.255.192- 64-9e-f3-b3-81-d3 -01/08/2018 17:07:46 -D- BH1-WAP-01.local
192.25.200.5 - 255.255.255.192- 50-57-a8-30-11-f6 -01/08/2018 16:24:59 -D- BH1-WAP-03.local
192.25.200.6 - 255.255.255.192- f0-f7-55-df-c4-ff -01/08/2018 16:45:04 -D- BH1-WAP-02.local
192.25.200.7 - 255.255.255.192- 00-23-24-bb-ce-83 -04/08/2018 10:55:00 -D- CESTA1703043.LOCAL
No of Clients(version 4): 4 in the Scope : 192.25.200.0.
Command completed successfully.
Changed the current scope context to 192.25.200.64 scope.
Type : N - NONE, D - DHCP B - BOOTP, U - UNSPECIFIED, R - RESERVATION IP
============================================================================================
IP Address - Subnet Mask - Unique ID - Lease Expires -Type -Name
============================================================================================
192.25.200.68 - 255.255.255.192- 28-94-0f-05-ac-6c -01/08/2018 03:14:46 -D- WAP-C12-01-1047.local
No of Clients(version 4): 1 in the Scope : 192.25.200.64.
Command completed successfully.
Changed the current scope context to 192.28.211.0 scope.
192.28.211.27 - 255.255.255.0 -00-0a-13-00-00-00-00- NEVER EXPIRES -U- CFCameraNearlockers
Changed the current scope context to 192.28.194.0 scope.
192.28.194.254 - 255.255.255.0 -00-48-32-95-85 - INACTIVE -N- bms
解决方案
您可以使用
'~^(\d{1,3}(?:\.\d{1,3}){3})\s*-\s*((?1))\s*-\s*((?:[0-9A-Fa-f]{2}[:-]){3,6}[0-9A-Fa-f]{2})\s*-\s*(\d\d/\d\d/\d{4}\s\d\d(?::\d\d){2}|NEVER EXPIRES|INACTIVE)\s*-\s*(\w)\s*-\s*(.*)$~m'
并替换为
'$1 ~ $2 ~ $3 ~ $4 ~ $5 ~ $6'
查看正则表达式演示
细节
^
- 一行的开始(\d{1,3}(?:\.\d{1,3}){3})
- 第 1 组:1 到 3 位数字,然后出现 3 次,.
后跟 1 到 3 位数字\s*-\s*
--
用 0+ 个空格括起来((?1))
- 第 2 组:捕获第 1 组模式\s*-\s*
--
用 0+ 个空格括起来((?:[0-9A-Fa-f]{2}[:-]){3,6}[0-9A-Fa-f]{2})
- 第 3 组:2 个十六进制字符出现 3 到 6 次,后跟:
or-
,然后是 2 个十六进制字符\s*-\s*
--
用 0+ 个空格括起来(\d\d/\d\d/\d{4}\s\d\d(?::\d\d){2}|NEVER EXPIRES|INACTIVE)
- 第 4 组:2 位数字、/
2 位数字/
、4 位数字、一个空格、2 位数字,然后出现 2 次:
和 2 位数字\s*-\s*
--
用 0+ 个空格括起来(\w)
- 第 5 组:一个单词 char\s*-\s*
--
用 0+ 个空格括起来(.*)
- 第 6 组:线路的其余部分$
- 行尾。
如果您只需要抓取这些行并重新格式化,请使用
$text = <YOUR TEXT>;
$reg = '~^(\d{1,3}(?:\.\d{1,3}){3})\s*-\s*((?1))\s*-\s*((?:[0-9A-Fa-f]{2}[:-]){3,6}[0-9A-Fa-f]{2})\s*-\s*(\d\d/\d\d/\d{4}\s\d\d(?::\d\d){2}|NEVER EXPIRES|INACTIVE)\s*-\s*(\w)\s*-\s*(.*)$~m';
$arr = [];
if (preg_match_all($reg, $text, $ms, PREG_SET_ORDER, 0)) {
foreach ($ms as $m) {
$arr[] = $m[1] . ' ~ ' . $m[2] . ' ~ ' . $m[3] . ' ~ ' . $m[4] . ' ~ ' . $m[5] . ' ~ ' . $m[6];
}
}
print_r($arr);
请参阅PHP 演示。
输出:
Array
(
[0] => 192.25.200.4 ~ 255.255.255.192 ~ 64-9e-f3-b3-81-d3 ~ 01/08/2018 17:07:46 ~ D ~ BH1-WAP-01.local
[1] => 192.25.200.5 ~ 255.255.255.192 ~ 50-57-a8-30-11-f6 ~ 01/08/2018 16:24:59 ~ D ~ BH1-WAP-03.local
[2] => 192.25.200.6 ~ 255.255.255.192 ~ f0-f7-55-df-c4-ff ~ 01/08/2018 16:45:04 ~ D ~ BH1-WAP-02.local
[3] => 192.25.200.7 ~ 255.255.255.192 ~ 00-23-24-bb-ce-83 ~ 04/08/2018 10:55:00 ~ D ~ CESTA1703043.LOCAL
[4] => 192.25.200.68 ~ 255.255.255.192 ~ 28-94-0f-05-ac-6c ~ 01/08/2018 03:14:46 ~ D ~ WAP-C12-01-1047.local
[5] => 192.28.211.27 ~ 255.255.255.0 ~ 00-0a-13-00-00-00-00 ~ NEVER EXPIRES ~ U ~ CFCameraNearlockers
[6] => 192.28.194.254 ~ 255.255.255.0 ~ 00-48-32-95-85 ~ INACTIVE ~ N ~ bms
)
推荐阅读
- amazon-web-services - 如何在应用期间使用 terraform 创建 ec2 后运行脚本?
- angular - 创建可重复使用的角垫自动完成
- javascript - 查找总计等于 N 的所有值组合(javascript)
- c# - 通过值更改触发事件 (
- python - 我想将一个表单的实例传递给另一个表单。我怎样才能做到这一点?
- python - 检查文本是否存在 2 个或多个用括号括起来的字符或数字,至少第一个字符为大写
- swift - 如果视图相同,则 TabView 生命周期问题
- javascript - Shortest Job First (Non preemptive) - 同时排序突发时间和到达时间
- javascript - 错误:无法获取 /
- javascript - Javascript html表格单元格编辑没有正确发生