如何从文本中提取 MAC 地址以获取每行只有一个 MAC 地址的列表?我尝试使用正则表达式标记 MAC 地址,为行添加书签并删除未标记的行,但周围仍然有太多文本。
这是我使用的正则表达式:
((([a-zA-z0-9]{2}[-:]){5}([a-zA-z0-9]{2}))|(([a-zA-z0-9]{2}:){5}([a-zA-z0-9]{2})))
答案1
由于十六进制不使用 f 后面的字母,因此您可以相应地更改 RE。[A-Fa-f0-9]
如果希望行中不包含其他文本,请使用^
和$
来指示行的开始和结束。
您的第二个子表达式似乎是第一个子表达式的子集并且是多余的。
答案2
此正则表达式将强制使用分隔符,因此不会匹配
-
和:
分隔符的混合(例如3D-F2-C9:A6-B3:4F
):
(?:[[:xdigit:]]{2}([-:]))(?:[[:xdigit:]]{2}\1){4}[[:xdigit:]]{2}
答案3
- Ctrl+H
- 找什么:
.*?(?<![-:a-f\d])(?|([a-f\d]{2}(?::[a-f\d]{2}){5})|([a-f\d]{2}(?:-[a-f\d]{2}){5}))(?![-:a-f\d]).*?
- 用。。。来代替:
$1\n
- 取消选中 相符
- 查看 环绕
- 查看 正则表达式
- 查看
. matches newline
- Replace all
解释:
.*? # 0 or more any character, not greedy
(?<![-:a-f\d]) # negative lookbehind, make sure we haven't hyphen, colon or hex before
(?| # branch reset group
( # group 1
[a-f\d]{2} # 2 characters in range a-fA-F0-9
(?: # non capture group
: # colon
[a-f\d]{2} # 2 characters in range a-fA-F0-9
){5} # end group, must appear 5 times
) # end group 1
| # OR
( # group 1
[a-f\d]{2} # 2 characters in range a-fA-F0-9
(?: # non capture group
- # hyphen
[a-f\d]{2} # 2 characters in range a-fA-F0-9
){5} # end group, must appear 5 times
) # end group 1
) # end branch reset group
(?![-:a-f\d]) # negative lookahead, make sure we haven't hyphen, colon or hex after
替代品:
$1 # content of group 1 (i.e. the mac address)
\n # linefeed, you can use \r\n for windows eol
截图(之前):
截图(之后):