只有在指定字符之前没有紧邻时才替换字符。

huangapple go评论85阅读模式
英文:

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 regexes 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 &lt;iostream&gt;
#include &lt;regex&gt;

int main() {
	std::string s{ &quot;A;1;\nB;2\nC;3&quot; };
	const std::regex re(&quot;([^;]|^)(\\n)&quot;);  // Produces &quot;A;1;;\nB;2;\nC;3&quot; (redundant &quot;;&quot; after &quot;1&quot;).
	s = std::regex_replace(s, re, &quot;$1;$2&quot;);
	std::cout &lt;&lt; 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.

huangapple
  • 本文由 发表于 2023年7月3日 21:23:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605180.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定