英文:
Replace Character Only if it isn't Immediately Preceded By Specified Character
问题
std::string s{ "A;1;\nB;2\nC;3" };
const std::regex re("([^;])\\n");
s = std::regex_replace(s, re, "$1;\n");
这将产生所需的输出:A;1;\nB;2;\nC;3
。
英文:
Given a multiple line string A;1;\nB;2\nC;3
. It's needed to add a ;
before those newline
characters that are not immediately preceded by ;
. So, the result should be A;1;\nB;2;\nC;3
(a single ;
is added after 2
). I've tried different regex
es but without luck (one of them is presented below). I think the lookbehind assertion would do the job, but it isn't supported.
Question: What regex
expression can solve this problem? If none, how to solve it using other methods using pure C++ only (I'm using C++20
)?
std::string s{ "A;1;\nB;2\nC;3" };
const std::regex re("(?!;)\\n"); // Produces "A;1;;\nB;2;\nC;3" (redundant ";" after "1").
s = std::regex_replace(s, re, ";\n");
答案1
得分: 1
你可以匹配并捕获字符串的起始位置或除了 ;
字符以外的任何字符,然后捕获换行字符,并使用反向引用替换匹配项:
#include <iostream>
#include <regex>
int main() {
std::string s{ "A;1;\nB;2\nC;3" };
const std::regex re("([^;]|^)(\\n)"); // 生成 "A;1;;\nB;2;\nC;3"("1" 后多了一个不必要的 ";")。
s = std::regex_replace(s, re, "$1;$2");
std::cout << s;
return 0;
}
输出:
A;1;
B;2;
C;3
模式详情:
([^;]|^)
- 第一组 ($1
):要么是除分号 (;
) 字符之外的字符 ([^;]
),要么 (|
) 是字符串的起始位置 (^
)(如果不需要匹配字符串开头的子串,则移除|^
)。(\\n)
- 第二组 ($2
):换行字符。
英文:
You can match and capture either a string start position or any character other than a ;
char, and then capture a newline char, and replace the matches using backreferences:
#include <iostream>
#include <regex>
int main() {
std::string s{ "A;1;\nB;2\nC;3" };
const std::regex re("([^;]|^)(\\n)"); // Produces "A;1;;\nB;2;\nC;3" (redundant ";" after "1").
s = std::regex_replace(s, re, "$1;$2");
std::cout << s;
return 0;
}
See the C++ demo and the regex demo.
Output:
A;1;
B;2;
C;3
Pattern details:
([^;]|^)
- Group 1 ($1
): either a char other than a semi-colon ([^;]
), or (|
) a start of the string position (^
) (remove|^
if there is no need to match substrings at the start of the string)(\\n)
- Group 2 ($2
): a newline char.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论