正则表达式替换为数组
regex replace with array
我目前正在编写一个脚本来匹配来自不同供应商的 IT 设备型号,想法是删除名称末尾的 -XXX 数字、结尾的 P 或 P-
示例模型是
DH-HAC-HDBW3802EP-Z HAC-HDBW3802E-Z
DH-HAC-HDBW3802EP-ZH HAC-HDBW3802E-ZH
DH-HAC-HDW1000MP-028 HAC-HDW1000M
DH-HAC-HDW1000RP-028 HAC-HDW1000R
DH-HAC-HDW1100EMP-02 HAC-HDW1100EM
DH-HAC-HDW1100EMP-03 HAC-HDW1100EM
DH-HAC-HDW1100MP HAC-HDW1100M
DH-HAC-HDW1100MP-036 HAC-HDW1100M
DH-HAC-HDW1100RP-028 HAC-HDW1100R
DH-HAC-HDW1100RP-VF HAC-HDW1100R-VF
现在我使用的是一个相当复杂的代码,我必须承认,它确实有效,但我内心深处有一种强烈的冲动要对它进行一点正则表达式*我知道,如果它有效,请不要乱用它*
清理名字结尾的函数看起来像
function beautifyDahua($text)
{
$text = str_replace('DHI-', '', $text);
$text = str_replace('DH-', '', $text);
if (empty($text)) {
return 'n-a';
}
//if begins with IPC sau HAC, clean further
elseif (substr( $text, 0, 4 ) === "IPC-" OR substr( $text, 0, 4 ) === "HAC-") {
$text = str_replace('AP-028', 'A', $text);
$text = str_replace('AP-036', 'A', $text);
$text = str_replace('AP', 'A', $text);
$text = str_replace('BP-028', 'B', $text);
$text = str_replace('BP-036', 'B', $text);
$text = str_replace('BP', 'B', $text);
$text = str_replace('CP-', 'C-', $text);
$text = str_replace('DP-036', 'D', $text);
$text = str_replace('DP-', 'D-', $text);
$text = str_replace('EMP-03', 'EM', $text);
$text = str_replace('EMP-02', 'EM', $text);
$text = str_replace('EMP-', 'EM-', $text);
$text = str_replace('EP-036', 'E', $text);
$text = str_replace('EP-028', 'E', $text);
$text = str_replace('EP-03', 'E', $text);
$text = str_replace('EP-02', 'E', $text);
$text = str_replace('EP-', 'E-', $text);
$text = str_replace('EP', 'E', $text);
$text = str_replace('FP-03', 'F', $text);
$text = str_replace('FP-02', 'F', $text);
$text = str_replace('FP-', 'F-', $text);
$text = str_replace('FP', 'F', $text);
$text = str_replace('RMP-03', 'RM', $text);
$text = str_replace('RMP-02', 'RM', $text);
$text = str_replace('RMP-', 'RM', $text);
$text = str_replace('RMP', 'RM', $text);
$text = str_replace('RP-028', 'R', $text);
$text = str_replace('RP-036', 'R', $text);
$text = str_replace('RP-', 'R-', $text);
$text = str_replace('RP', 'R', $text);
$text = str_replace('SP-036', 'S', $text);
$text = str_replace('SP-028', 'S', $text);
$text = str_replace('SP-', 'S-', $text);
$text = str_replace('SP', 'S', $text);
$text = str_replace('SLP-03', 'SL', $text);
$text = str_replace('TP-', 'T-', $text);
$text = str_replace('MP-036', 'M', $text);
$text = str_replace('MP-028', 'M', $text);
$text = str_replace('MP', 'M', $text);
return $text;
}
else {
return $text;
}
}
对于数字,我有一个像 \b-0(\d|\d\d)\b
这样的正则表达式
但是对于P的情况,我有点头疼了。
关于如何解决这个问题有什么建议吗?
这里有一些东西,但不确定它是否适合你:
preg_replace("/\b(DH-)?(HAC-)(\w+\d+)(\w)(\w*)(-?\d+)?/", "", $input_lines);
所以基本上它匹配带有可选 DH- 后跟 HAC- 后跟任意数量字母后跟任意数量数字,后跟字母(至少 2 个可选后跟 -numbers
的单词
这是一个有点棘手的部分,因为结尾可以选择匹配 -\d+
但不在替换中使用它,它将删除它但它不匹配 -\w
所以如果尾随字符存在它们将被保留。但是,如果这是句子的一部分,这将失败。
您的数字正则表达式 \b-0(\d|\d\d)\b
可以写成 -0\d{1,2}
。对于这场比赛,我认为您不需要边界 \b
.
这个词
像这样尝试:
(?:DHI?-)?(?:IPC|HAC)-HDB?W\d+[A-Z]+\K(?:P-0\d{1,2}|P)
正则表达式使用 \K
重置报告匹配的起点并匹配后面的内容。
然后您可以用空字符串替换所选匹配项。
说明
(?:
非捕获组
DHI?-
匹配 DH 与可选大写 I
)?
关闭非捕获组
(?:
非捕获组
IPC|HAC
匹配IPC或HAC
)
关闭非捕获组
-HDB?W
匹配dash HD,可选B和W
\d+
匹配一位或多位数字
[A-Z]+
匹配一个或多个大写字符
\K
重置报告比赛的起点
(?:
非捕获组 (这将包含您的匹配项)
P-
匹配P-
0\d{1,2}
匹配0和2位数字(或\d{2,3}
匹配2或3位数字)
|
或
P
匹配P
)
关闭非捕获组
这是我推荐给你的正则表达式:
Pattern: (?:DHI?-)?((?:HAC|IPC)-[A-Z0-9]+)(?:P-\d+|P)
Replacement:
和他的 PHP
实现使用 preg_replace function:
$text = 'DH-HAC-HDW1000MP-028';
$result = preg_replace('/(?:DHI?-)?((?:HAC|IPC)-[A-Z0-9]+)(?:P-\d+|P)/', '', $text);
echo $result; // HAC-HDW1000M
您可以访问 this link 查看工作演示。
在弄乱了@apokryfos 解决方案后,我来到了
$text = preg_replace("/\b(DHI-|DH-)?(HAC-|IPC-)(\w+\d+)(\w(M|L)?)(P)(\w*)(-?\d+)?/", "", $text);
$text = preg_replace("/\b(DHI-|DH-)?/", "", $text);
但我看到 Thomassos 解决方案开箱即用,我将不得不检查我拥有的 1200 多个示例,看看哪个最适合我的情况,无论如何,非常感谢您的支持。
我目前正在编写一个脚本来匹配来自不同供应商的 IT 设备型号,想法是删除名称末尾的 -XXX 数字、结尾的 P 或 P- 示例模型是
DH-HAC-HDBW3802EP-Z HAC-HDBW3802E-Z
DH-HAC-HDBW3802EP-ZH HAC-HDBW3802E-ZH
DH-HAC-HDW1000MP-028 HAC-HDW1000M
DH-HAC-HDW1000RP-028 HAC-HDW1000R
DH-HAC-HDW1100EMP-02 HAC-HDW1100EM
DH-HAC-HDW1100EMP-03 HAC-HDW1100EM
DH-HAC-HDW1100MP HAC-HDW1100M
DH-HAC-HDW1100MP-036 HAC-HDW1100M
DH-HAC-HDW1100RP-028 HAC-HDW1100R
DH-HAC-HDW1100RP-VF HAC-HDW1100R-VF
现在我使用的是一个相当复杂的代码,我必须承认,它确实有效,但我内心深处有一种强烈的冲动要对它进行一点正则表达式*我知道,如果它有效,请不要乱用它* 清理名字结尾的函数看起来像
function beautifyDahua($text)
{
$text = str_replace('DHI-', '', $text);
$text = str_replace('DH-', '', $text);
if (empty($text)) {
return 'n-a';
}
//if begins with IPC sau HAC, clean further
elseif (substr( $text, 0, 4 ) === "IPC-" OR substr( $text, 0, 4 ) === "HAC-") {
$text = str_replace('AP-028', 'A', $text);
$text = str_replace('AP-036', 'A', $text);
$text = str_replace('AP', 'A', $text);
$text = str_replace('BP-028', 'B', $text);
$text = str_replace('BP-036', 'B', $text);
$text = str_replace('BP', 'B', $text);
$text = str_replace('CP-', 'C-', $text);
$text = str_replace('DP-036', 'D', $text);
$text = str_replace('DP-', 'D-', $text);
$text = str_replace('EMP-03', 'EM', $text);
$text = str_replace('EMP-02', 'EM', $text);
$text = str_replace('EMP-', 'EM-', $text);
$text = str_replace('EP-036', 'E', $text);
$text = str_replace('EP-028', 'E', $text);
$text = str_replace('EP-03', 'E', $text);
$text = str_replace('EP-02', 'E', $text);
$text = str_replace('EP-', 'E-', $text);
$text = str_replace('EP', 'E', $text);
$text = str_replace('FP-03', 'F', $text);
$text = str_replace('FP-02', 'F', $text);
$text = str_replace('FP-', 'F-', $text);
$text = str_replace('FP', 'F', $text);
$text = str_replace('RMP-03', 'RM', $text);
$text = str_replace('RMP-02', 'RM', $text);
$text = str_replace('RMP-', 'RM', $text);
$text = str_replace('RMP', 'RM', $text);
$text = str_replace('RP-028', 'R', $text);
$text = str_replace('RP-036', 'R', $text);
$text = str_replace('RP-', 'R-', $text);
$text = str_replace('RP', 'R', $text);
$text = str_replace('SP-036', 'S', $text);
$text = str_replace('SP-028', 'S', $text);
$text = str_replace('SP-', 'S-', $text);
$text = str_replace('SP', 'S', $text);
$text = str_replace('SLP-03', 'SL', $text);
$text = str_replace('TP-', 'T-', $text);
$text = str_replace('MP-036', 'M', $text);
$text = str_replace('MP-028', 'M', $text);
$text = str_replace('MP', 'M', $text);
return $text;
}
else {
return $text;
}
}
对于数字,我有一个像 \b-0(\d|\d\d)\b
这样的正则表达式
但是对于P的情况,我有点头疼了。
关于如何解决这个问题有什么建议吗?
这里有一些东西,但不确定它是否适合你:
preg_replace("/\b(DH-)?(HAC-)(\w+\d+)(\w)(\w*)(-?\d+)?/", "", $input_lines);
所以基本上它匹配带有可选 DH- 后跟 HAC- 后跟任意数量字母后跟任意数量数字,后跟字母(至少 2 个可选后跟 -numbers
的单词这是一个有点棘手的部分,因为结尾可以选择匹配 -\d+
但不在替换中使用它,它将删除它但它不匹配 -\w
所以如果尾随字符存在它们将被保留。但是,如果这是句子的一部分,这将失败。
您的数字正则表达式 \b-0(\d|\d\d)\b
可以写成 -0\d{1,2}
。对于这场比赛,我认为您不需要边界 \b
.
像这样尝试:
(?:DHI?-)?(?:IPC|HAC)-HDB?W\d+[A-Z]+\K(?:P-0\d{1,2}|P)
正则表达式使用 \K
重置报告匹配的起点并匹配后面的内容。
然后您可以用空字符串替换所选匹配项。
说明
(?:
非捕获组DHI?-
匹配 DH 与可选大写 I
)?
关闭非捕获组(?:
非捕获组IPC|HAC
匹配IPC或HAC
)
关闭非捕获组-HDB?W
匹配dash HD,可选B和W\d+
匹配一位或多位数字[A-Z]+
匹配一个或多个大写字符\K
重置报告比赛的起点(?:
非捕获组 (这将包含您的匹配项)P-
匹配P-0\d{1,2}
匹配0和2位数字(或\d{2,3}
匹配2或3位数字)|
或P
匹配P
)
关闭非捕获组
这是我推荐给你的正则表达式:
Pattern: (?:DHI?-)?((?:HAC|IPC)-[A-Z0-9]+)(?:P-\d+|P)
Replacement:
和他的 PHP
实现使用 preg_replace function:
$text = 'DH-HAC-HDW1000MP-028';
$result = preg_replace('/(?:DHI?-)?((?:HAC|IPC)-[A-Z0-9]+)(?:P-\d+|P)/', '', $text);
echo $result; // HAC-HDW1000M
您可以访问 this link 查看工作演示。
在弄乱了@apokryfos 解决方案后,我来到了
$text = preg_replace("/\b(DHI-|DH-)?(HAC-|IPC-)(\w+\d+)(\w(M|L)?)(P)(\w*)(-?\d+)?/", "", $text);
$text = preg_replace("/\b(DHI-|DH-)?/", "", $text);
但我看到 Thomassos 解决方案开箱即用,我将不得不检查我拥有的 1200 多个示例,看看哪个最适合我的情况,无论如何,非常感谢您的支持。