正则表达式中字符串匹配

查看:158
本文介绍了正则表达式中字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我需要提取出一些特定的URL,在写正则时碰到一个问题:
URL样例:

127.0.0.1 - - [14/Jul/2016:10:10:18 +0800] "GET /webshell/webshellSample-master/PHP/dama/0da3e2aa1745a397921ef9c7dcc72d6e.php?y=C:\\AppServ\\www\\webshell\\webshellSample-master\\PHP\\dama\\&x=upload HTTP/1.1" 200 5867
127.0.0.1 - - [14/Jul/2016:10:10:28 +0800] "GET /webshell/webshellSample-master/PHP/dama/0da3e2aa1745a397921ef9c7dcc72d6e.php?y=C:\\AppServ\\www\\webshell\\webshellSample-master\\PHP\\dama\\&x=mail HTTP/1.1" 200 4984

我写的正则表达式为(.*)/0da3e2aa1745a397921ef9c7dcc72d6e.php?(.*)&x=[upload|mail]
匹配结果:

127.0.0.1 - - [14/Jul/2016:10:10:18 +0800] "GET /webshell/webshellSample-master/PHP/dama/0da3e2aa1745a397921ef9c7dcc72d6e.php?y=C:\\AppServ\\www\\webshell\\webshellSample-master\\PHP\\dama\\&x=u
127.0.0.1 - - [14/Jul/2016:10:10:28 +0800] "GET /webshell/webshellSample-master/PHP/dama/0da3e2aa1745a397921ef9c7dcc72d6e.php?y=C:\\AppServ\\www\\webshell\\webshellSample-master\\PHP\\dama\\&x=m

后来正则表达式改为:(.*)/0da3e2aa1745a397921ef9c7dcc72d6e.php?(.*)&x=[upload|mail](.*)就能匹配出想要的结果

127.0.0.1 - - [14/Jul/2016:10:10:18 +0800] "GET /webshell/webshellSample-master/PHP/dama/0da3e2aa1745a397921ef9c7dcc72d6e.php?y=C:\\AppServ\\www\\webshell\\webshellSample-master\\PHP\\dama\\&x=upload HTTP/1.1" 200 5867
127.0.0.1 - - [14/Jul/2016:10:10:28 +0800] "GET /webshell/webshellSample-master/PHP/dama/0da3e2aa1745a397921ef9c7dcc72d6e.php?y=C:\\AppServ\\www\\webshell\\webshellSample-master\\PHP\\dama\\&x=mail HTTP/1.1" 200 4984

很疑惑为什么第一个正则表达式只匹配[|]格式中的第一个首字母

解决方案

原因是[upload|mail]
不是匹配uploadmail

而是匹配u, p, l, o, a, d, |, m, a, il中的一个即可

想达到你原来的效果可以改为括号

(.*)/0da3e2aa1745a397921ef9c7dcc72d6e.php?(.*)&x=(upload|mail)

这篇关于正则表达式中字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆