我有一个DNS日志,如下所示:
17/04/2017 08:59:38 09DC PACKET 000000A939E64600 UDP Rcv 10.254.30.96 c650 Q [0001 D NOERROR] A (8)master11(10)teamviewer(3)com(0)
17/04/2017 08:59:38 09DC PACKET 000000A9404FABE0 UDP Rcv 10.254.30.87 9bae Q [0001 D NOERROR] A (7)master2(10)teamviewer(3)com(0)
17/04/2017 08:59:42 09E0 PACKET 000000A9404D15B0 UDP Rcv 10.254.35.43 fdcc Q [0001 D NOERROR] A (6)mobile(4)pipe(4)aria(9)microsoft(3)com(0)
17/04/2017 09:49:45 09E8 PACKET 000000A94194FE80 UDP Rcv 8.8.8.8 646d R Q [8381 DR NXDOMAIN] A (3)mta(3)dkf(2)com(0)
在每一行的末尾,您将看到如下所示的域名:
(7)master2(10)teamviewer(3)com(0)
我想只获得以下部分:
master teamviewer com
并用“。”替换空格。得到这样的结果:
master2.teamviewer.com
为此,我使用了这个正则表达式:
(?<=\)).*?(?=\()
但不幸的是我收到了这个错误:
Lookbehind is not supported in javascript
是否有人对此问题有任何建议或解决方案? 谢谢
答案 0 :(得分:1)
您可以使用this article中的解决方案:
A\s+\(\d+\)(.*?)\(\d+\)(.*?)\(\d+\)(.*?)\(\d+\)
请参阅regex demo
在这里,
A
- 文字A
\s+
- 1+空格\(\d+\)
- 包含(
和)
(.*?)
- 任意0个字符,尽可能少(因为*?
是一个懒惰的量词)\(\d+\)(.*?)\(\d+\)(.*?)\(\d+\)
- 上述两个子模式的交替序列。根据需要设置捕获组选项。在上面的例子中,
Format String : $1.$2.$3
答案 1 :(得分:0)
你可以试试这个:
\(\d+\)(?:[\w]+\(\d+\))+
const regex = /\(\d+\)(?:[\w]+\(\d+\))+/g;
const str = `17/04/2017 08:59:38 09DC PACKET 000000A939E64600 UDP Rcv 10.254.30.96 c650 Q [0001 D NOERROR] A (8)master11(10)teamviewer(3)com(0)
17/04/2017 08:59:38 09DC PACKET 000000A9404FABE0 UDP Rcv 10.254.30.87 9bae Q [0001 D NOERROR] A (7)master2(10)teamviewer(3)com(0)
17/04/2017 08:59:42 09E0 PACKET 000000A9404D15B0 UDP Rcv 10.254.35.43 fdcc Q [0001 D NOERROR] A (6)mobile(4)pipe(4)aria(9)microsoft(3)com(0)
17/04/2017 09:49:45 09E8 PACKET 000000A94194FE80 UDP Rcv 8.8.8.8 646d R Q [8381 DR NXDOMAIN] A (3)mta(3)dkf(2)com(0)
`;
let m;
while ((m = regex.exec(str)) !== null) {
console.log(m[0].split(/\(\d+\)/).filter(val => val).join("."));
}