[英]In SpectreConsole, how to strip the tags to get a plain text string?
我正在使用Spectre.Console
並且有很多這樣的AnsiConsole.MarkupLine
命令:
AnsiConsole.MarkupLine($"[lime]File size:[/] [bold]\t{file.Length,-10}[/]");
我想要 output 相同的純文本文本,文本文件中沒有顏色,比如
var msg = $"[lime]File size:[/] [bold]\t{file.Length,-10}[/]"
AnsiConsole.MarkupLine(msg);
var msgclean = AnsiConsole.StripTag(msg);
LogToFile(msgclean);
有沒有辦法去除標簽? 或者以某種方式將控制台 output 重定向到文件?
我仔細研究了他們的 API,發現這個擴展方法應該可以解決問題: RemoveMarkup()
https://spectreconsole.net/api/spectre.console/stringextensions/29bfb33e
看看以下語言擴展是否適合您。 取自這篇文章的正則表達式模式。 通過dotnetfiddle嘗試。
public static class StringExtensions
{
private static readonly Regex Whitespace = new(@"\s+");
public static string Flatten(this string value)
=> value is null or "" ?
value :
Whitespace.Replace(value.Trim(), " ");
public static string StripCodes(this string sender)
=> Regex.Replace(sender, @"\[[^]]*\]", "")
.Flatten();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.