在 CPP 中使用正则表达式将密码("ABC")子句屏蔽为密码("****")
masking the password('abcs') clause to password('****') using regex in cpp
我有一个转换字符串的函数:
select * from run_on_hive(server('hdp230m2.labs.teradata.com'),username('vijay'),password('vijay'),dbname('default'),query('analyze table default.test01 compute statistics'));
自:
select * from run_on_hive(server('hdp230m2.labs.teradata.com'),username('vijay'),'****',dbname('default'),query('analyze table default.test01 compute statistics'));
该函数如下所示:
static SimpleRegexMask::Ptr newUDFMask(const String &udfName, const int paramPos)
{
return SimpleRegexMask::newInstance(
udfName,
udfName + "([^[:alpha:]]*)\((([^,]*,){" + toString(paramPos - 1) + "})([^,]*)(,[^\)]*)\)",
udfName + "\1\(\2'****'\5\)"
);
}
这些是上面函数中的功能。我希望它能解释我想做什么
static Ptr newInstance(
const String &baseRegex,
const String &replaceRegex,
const String &matchFormatString
)
{
return new SimpleRegexMask(baseRegex, replaceRegex, matchFormatString);
}
SimpleRegexMask::SimpleRegexMask(
const String &baseRegex,
const String &replaceRegex,
const String &matchFormatString
)
{
try {
basePattern_ = boost::regex(
baseRegex, boost::regex_constants::icase|boost::regex_constants::perl
);
replacePattern_ = boost::regex(
replaceRegex, boost::regex_constants::icase|boost::regex_constants::perl
);
matchFormatString_ = matchFormatString;
} catch (const boost::regex_error& ex) {
// programming error i.e. the regex supplied is not valid
NOT_REACHED;
}
}
但是,我想将字符串修改为
select * from run_on_hive(server('hdp230m2.labs.teradata.com'),username('vijay'),password('****'),dbname('default'),query('analyze table default.test01 compute statistics'));
我该如何修改上述函数来做到这一点?我哪里错了。请让我知道。 蒂亚。
您可以使用
return SimpleRegexMask::newInstance(
udfName,
"(" + udfName + "[^[:alpha:]]*)\(((?:[^,]*,){" + toString(paramPos - 1) + "}[^,(]*\(['"])[^,'"]*(['"]\),[^)]*)\)",
"\1\(\2****\3\)"
);
请参阅正则表达式演示。
我在这里优化了捕获组的使用(减少它们的数量(,并使用[^,(]*\(['"])[^,'"]*(['"]\)
模式以以下方式匹配password
部分:捕获password('
或password("
到组 2,然后只匹配除,
、"
或'
以外的任何 0+ 字符,然后将')
或")
捕获到后续捕获组中。
请注意,UDF 名称被捕获到组 1 中,您无需在替换字符串中对其进行硬编码。因此,如果它在字符串中RUN_ON_HIVE
,即使您在模式中有run_on_hive
,它也会在结果中RUN_ON_HIVE
(因为您使用的是不区分大小写的修饰符(。
相关文章:
- 如何理解C++标准N3337中的expr.const.cast子句8
- c++中的oop(密码生成)
- 密码登录程序将永远循环并显示不正确的结果
- if 子句中的赋值不起作用
- OpenSSL没有共享密码
- C++20 概念:如何在"requires"子句中引用类名?
- Cryptopp:获取密码输入的填充字符串
- 密码长度验证 (c++)
- 尝试将另一个子句添加到代码中时出错
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 仿射密码解密,输出大小写不同
- 替换密码:哪一个?
- 对OpenMP reduction子句中的变量执行原子操作
- 无法使用 openssl 服务器,因为密码已过时...什么的...ERR_SSL_VERSION_OR_CIPHER_MISMATCH
- 如何使用 c++ 在 sqlite3 中打开受密码保护的数据库?
- 根据用户名和密码 Qt C++重新访问数据库值
- C++密码问题
- 无法在 Cocos2dx 中使用受密码保护的 zip 文件
- 如何使用librdkafka设置明文协议(无SASL)的用户名和密码?
- 在 CPP 中使用正则表达式将密码("ABC")子句屏蔽为密码("****")