js,c/c++生成随机字符(去除易混淆字符)
1.javascript
// 获取长度为len的随机字符串
function _getRandomString(len) {
len = len || 32;
var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
var maxPos = $chars.length;
var pwd = '';
for (i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
2.C/C++
示例用法:
- C/C++ code
-
~# echo "\w{3,16}"| regxstr 10
作用为产生10个符合正则表达式“\w{3,16}”的字符串
同时提供头文件,静态库和共享库,使用方法为:
- C/C++ code
-
#include <iostream> #include <regxstring.h>usingnamespace std; int main() { constchar* expr ="\\w{3,16}"; CRegxString regxstr; regxstr.ParseRegx(expr); for(int i =0;i <10;++i) cout<<regxstr.RandString()<<endl; return0; }
试用了一下,用下面的表示IP地址的正则表达式:
- C/C++ code
-
^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$
产生了下面的结果:
- C/C++ code
-
251.232.237.12253.222.254.209229.211.213.252230.253.202.220074.6.255.25382.255.223.252210.214.238.24