惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
J
Java Code Geeks
V
V2EX
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
B
Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
美团技术团队
大猫的无限游戏
大猫的无限游戏
爱范儿
爱范儿
N
Netflix TechBlog - Medium
C
Check Point Blog
Recent Announcements
Recent Announcements
博客园 - Franky
博客园 - 叶小钗
T
Tailwind CSS Blog

博客园 - wgscd

Git 无法访问 GitHub(unable to access ‘https://github.com/.../.git‘)问题解决教程 RapidOcrNet文字识别OCR C#+Audio绘制麦克风波形 一共免费大模型API AI聊天对话界面的HTML代码。 纯JavaScript代码实现保存Canvas图片到电脑 WPF鼠标拖动改变图片透明度 Fiddler自定义规则保存图片和提示The system proxy was changed自动重连 缩放 div 浏览器语音识别-webkitSpeechRecognition HTML获取摄像头画面,拍照截图保存 JS摄像头手势识别-Handsfree.js HTML,JS 模拟聊天界面UI JS获取元素相对窗口的位置和大小 C# 判断是否安装了ffmpeg shadow-root中的元素定位方法 JS MutationObserver监听DOM元素改变 Webview2动态设置页面video的Blob进行播放 C# 文件分割和文件合并 WPF刮刮乐 JS利用浏览器进行语言识别 WPF设置默认语言地区CultureInfo VS2022推送代码 到github错误: CertGetCertificateChain trust error CERT_TRUST_IS_PARTIAL_CHAIN的解决办法 使用ffmpeg去除音频静音
C#正则表达式匹配候选词
wgscd · 2024-12-02 · via 博客园 - wgscd

来自文心一言(多次修改才正确的):

    public App()
    {



        string input = "例子文字{备选,:'词1t324|备选词2gdfg,该方法|备选词3dsfdsf}继续{备选..*&fdgfd54词A|备选词.*&fdgfd54B|备选词.*&fdgfd54C}话术内容";
        string result = ReplaceAlternatives(input);
        Console.WriteLine(result);
    }
    static string ReplaceAlternatives(string input)
    {
        string pattern = @"\{([^}]+)\}";
        MatchCollection matches = Regex.Matches(input, pattern);

        Random random = new Random();
        StringBuilder sb = new StringBuilder(input);
        int offset = 0; // 用于跟踪已经进行的替换导致的索引偏移

        foreach (Match match in matches)
        {
            // 由于之前的替换,我们需要调整当前匹配项的索引
            int adjustedIndex = match.Index - offset;
            string alternatives = match.Groups[1].Value;
            string[] options = alternatives.Split('|');

            int randomIndex = random.Next(options.Length);
            string chosenOption = options[randomIndex];

            // 计算要移除的字符串长度(即匹配项的长度)
            int removeLength = match.Length;

            // 执行替换
            sb.Remove(adjustedIndex, removeLength);
            sb.Insert(adjustedIndex, chosenOption);

            // 更新偏移量
            offset += removeLength - chosenOption.Length;
        }

        return sb.ToString();
    }
}

fffffffffffffffff

test red font.

posted @ 2024-12-02 08:46  wgscd  阅读(28)  评论()    收藏  举报