You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:正则表达式实现星号替换为<bold>标签,支持多星号与奇数星号场景

Solution for Asterisk-to-Bold Tag Conversion with Edge Cases

Let's fix this regex issue properly—your current approach misses handling for consecutive asterisks and unclosed pairs, leading to unexpected results (and that crash you mentioned). Here's a straightforward, two-step solution that meets all your requirements:

Core Rules We're Implementing

  • Consecutive asterisks count as a single delimiter
  • Only wrap content in <bold> tags if there's a matching closing set of asterisks
  • Unclosed consecutive asterisks get collapsed to a single asterisk instead of being ignored entirely

Step-by-Step Implementation

1. Replace Paired Asterisk Groups with Bold Tags

First, we'll target all content wrapped between two sets of consecutive asterisks (any number of asterisks on either side). This regex safely captures the content between them and swaps the asterisks for your tags:

Regex boldPairRegex = new Regex(@"\*+([^*]+?)\*+");
  • \*+: Matches one or more consecutive asterisks (our start delimiter)
  • ([^*]+?): Non-greedily captures any characters that aren't asterisks (this is the content we want to bold)
  • \*+: Matches another set of one or more consecutive asterisks (our end delimiter)

Replace matches with: <bold>$1</bold> where $1 refers to the captured content.

2. Collapse Unclosed Consecutive Asterisks

After handling paired groups, we'll clean up any remaining consecutive asterisks (the unclosed ones) by collapsing them to a single asterisk:

Regex singleStarRegex = new Regex(@"\*+");

Replace matches with a single *.

Full C# Code Example

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string exampleText1 = "**** PLEASE NOTE *** Testing, *nuts*, **please note..., test";
        string exampleText2 = "**Test text (10)";

        // Step 1: Process paired asterisk groups
        string step1Result1 = new Regex(@"\*+([^*]+?)\*+").Replace(exampleText1, @"<bold>$1</bold>");
        string step1Result2 = new Regex(@"\*+([^*]+?)\*+").Replace(exampleText2, @"<bold>$1</bold>");

        // Step 2: Clean up unclosed consecutive asterisks
        string finalResult1 = new Regex(@"\*+").Replace(step1Result1, @"*");
        string finalResult2 = new Regex(@"\*+").Replace(step1Result2, @"*");

        // Verify results
        Console.WriteLine("Result for exampleText1:");
        Console.WriteLine(finalResult1);
        // Output: <bold> PLEASE NOTE </bold> Testing, <bold>nuts</bold>, *please note..., test

        Console.WriteLine("\nResult for exampleText2:");
        Console.WriteLine(finalResult2);
        // Output: *Text text (10)
    }
}

Why This Works

  • For exampleText1, the paired asterisk groups (**** ... *** and *nuts*) get converted to bold tags first. The remaining ** (unclosed) gets collapsed to a single *.
  • For exampleText2, since there's no closing set of asterisks, the first regex doesn't match anything. The ** then gets collapsed to a single *, avoiding any crashes and meeting your expected output.

This approach is easy to read, maintain, and handles all the edge cases you mentioned without any unexpected behavior.

内容的提问来源于stack exchange,提问作者William Armstrong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 00:37:28