@@ -18,8 +18,20 @@ namespace {
1818 * @return std::string Reformatted replace pattern
1919 */
2020std::string reformat_replace_pattern (std::string replace_pattern) {
21- return std::regex_replace (replace_pattern, std::regex (R"( (\\)([0-9]+))" ), R"( $$2)" );
21+ return std::regex_replace (replace_pattern, std::regex (R"( (?:\\)([0-9]+))" ), R"( $$1)" );
22+ }
2223
24+ /* *
25+ * @brief Fix old search pattern for backward compatibility
26+ *
27+ * @param search_pattern Search pattern to replace
28+ * @return std::string Replaced search pattern
29+ */
30+ std::string fix_search_pattern (std::string search_pattern) {
31+ if (search_pattern == R"( ([\\.\\?\\!,])| ('[ms])| (') | ('[rv]e)| (n't))" ) {
32+ return R"( (?| ([\\.\\?\\!,])| ('[ms])| (') | ('[rv]e)| (n't)))" ;
33+ }
34+ return search_pattern;
2335}
2436
2537} // namespace
@@ -36,8 +48,10 @@ m_global_replace(global_replace) {
3648 auto replace_pattern_const = as_type_ptr<Constant>(arguments[pattern_input + 1 ].get_node_shared_ptr ());
3749 auto search_pattern_buf = static_cast <const char *>(search_pattern_const->get_data_ptr ());
3850 auto replace_pattern_buf = static_cast <const char *>(replace_pattern_const->get_data_ptr ());
39- auto search_pattern = std::string (search_pattern_buf, search_pattern_const->get_byte_size ());
40- m_replace_pattern = std::string (replace_pattern_buf, replace_pattern_const->get_byte_size ());
51+ auto search_pattern = fix_search_pattern (std::string (search_pattern_buf, search_pattern_const->get_byte_size ()));
52+ m_replace_pattern = reformat_replace_pattern (
53+ std::string (replace_pattern_buf, replace_pattern_const->get_byte_size ())
54+ );
4155
4256 m_search_pattern_pcre2 = std::make_shared<PCRE2Wrapper>(search_pattern);
4357
@@ -66,7 +80,7 @@ RegexNormalization::RegexNormalization(
6680 if (m_search_pattern_pcre2 == nullptr ) {
6781 search_pattern_buf = static_cast <const char *>(search_pattern_const->get_data_ptr ());
6882 replace_pattern_buf = static_cast <const char *>(replace_pattern_const->get_data_ptr ());
69- search_pattern = std::string (search_pattern_buf, search_pattern_const->get_byte_size ());
83+ search_pattern = fix_search_pattern ( std::string (search_pattern_buf, search_pattern_const->get_byte_size () ));
7084 m_replace_pattern = std::string (replace_pattern_buf, replace_pattern_const->get_byte_size ());
7185 m_replace_pattern = reformat_replace_pattern (m_replace_pattern);
7286 m_search_pattern_pcre2 = std::make_shared<PCRE2Wrapper>(search_pattern);
@@ -100,7 +114,9 @@ bool RegexNormalization::evaluate(ov::TensorVector& outputs, const ov::TensorVec
100114 // Write to common trie structures should be protected to prevent race conditions.
101115 std::lock_guard<std::mutex> lock (m_mutex);
102116 if (m_search_pattern_pcre2 == nullptr ) {
103- std::string search_pattern = std::string (inputs[pattern_input].data <const char >(), inputs[pattern_input].get_size ());
117+ std::string search_pattern = fix_search_pattern (
118+ std::string (inputs[pattern_input].data <const char >(), inputs[pattern_input].get_size ())
119+ );
104120 m_replace_pattern = std::string (inputs[pattern_input + 1 ].data <const char >(), inputs[pattern_input + 1 ].get_size ());
105121 m_replace_pattern = reformat_replace_pattern (m_replace_pattern);
106122 m_search_pattern_pcre2 = std::make_shared<PCRE2Wrapper>(search_pattern);
0 commit comments