[已解决]多关键字排除正则案例
本帖最后由 elexy 于 2011-10-13 15:10 编辑需求:每台打印机后面的 | 为分隔符。只认出本机安装的真实打印机,网络共享来的及虚拟的都不要。
规律: 排除包含以下四种关键字的字段以及后面的 | 分隔符 1. Microsoft2. PDF3. fax4. \\ 不区分大小写,目前已知这四种,最好是开放式的,以后能修改或添加关键字。
案例模拟结果:提取出来 EPSON LQ680K|Canon IP1800| 即可。
Local $str = "EPSON LQ680K|Microsoft XPS Document Writer|Fax|Microsoft Office Document Image Writer|Foxit PDF|\\192.168.1.2\hp6L|Canon IP1800|" #include <Array.au3>
Local $Str = 'EPSON LQ680K|Microsoft XPS Document Writer|Fax|Microsoft Office Document Image Writer|Foxit PDF|\\192.168.1.2\hp6L|Canon IP1800|' & @CRLF
;MsgBox(0, '原字符串', $Str)
Local $pc = 'Microsoft|PDF|fax|\\\\' ;可继续加入其它排除字符
Local $Test = StringRegExp($str, '(?i)(?<=^|\|)(?:(?!' & $pc & ').)+?(?=\|)', 3)
_ArrayDisplay($Test, UBound($Test))
如果直接要 “EPSON LQ680K|Canon IP1800|” 这个结果就用替换模式~Local $Str = 'EPSON LQ680K|Microsoft XPS Document Writer|Fax|Microsoft Office Document Image Writer|Foxit PDF|\\192.168.1.2\hp6L|Canon IP1800|'
;MsgBox(0, '原字符串', $Str)
$pc = 'Microsoft|PDF|fax|\\\\'
Local $Test = StringRegExpReplace($str, '(?i)[^\|]*?(?:' & $pc & ')[^\|]*?\|', '')
MsgBox(0, '替换结果', $Test)
回复 3# afan
afan版主的正则真是太厉害了,佩服得不行了! afan第一种方法包头,牵涉数组,不太精简,第二种方法非常好!!!谢谢!!!
页:
[1]