在 CPP 中使用正则表达式将密码("ABC")子句屏蔽为密码("****")

masking the password('abcs') clause to password('****') using regex in cpp

本文关键字:密码 子句 屏蔽 CPP ABC 正则表达式      更新时间:2023-10-16

我有一个转换字符串的函数:

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(因为您使用的是不区分大小写的修饰符(。