Home > other >  Regex expression to bold using asterisks
Regex expression to bold using asterisks

Time:11-09

I have a question regarding using a regular expression to bold text within a string using asterisks.

The other questions on this topic work well for simple scenarios however we have encountered some issues.

Our particular scenario is for asterisks to be replaced with <bold></bold> tags. It must also be able to handle multiple asterisks as well as an uneven number of asterisks.

Our example input text is as follows;

string exampleText1 = "**** PLEASE NOTE *** Testing, *nuts*, **please note..., test";

string exampleText2 = "**Test text (10)";

Our current regex is as follows;

Regex _boldRegex = new Regex(@"(\*) ([^*?$] ) (\*)");

string value = _boldRegex.Replace(exampleText1, @"<bold>$2</bold>");

Example 1 should show "<bold> PLEASE NOTE </bold> Testing, <bold>nuts</bold>, *please note..., test" where the groups of asterisks are treated as single asterisks and an unfinished tag is ignored.

Example 2 crashes the program because it expects a 'closing' asterisk. It should show "*Text text (10)"

Can anyone help by suggesting a new regex, bearing in mind the ability to handle groups of asterisks and also an uneven number of asterisks?

Thanks in advance.

CodePudding user response:

For you examle data, you might use an optional part with a capture group to capture the repeated character class without newlines between 1 or more *

In the callback of replace, you can test for the existence of group 1, and do the replacements based on that.

\* (?:([^*?$\n\r] )\* )?

The pattern matches:

  • \* Match 1 times *
  • (?: Non capture group
    • ( Capture group 1
      • [^*?$\n\r] Match 1 times any char other than the listed in the character class
    • ) Close group 1
    • \* Match 1 times *
  • )? Close on capture group

See a regex demo.

For example

Regex _boldRegex  = new Regex(@"\* (?:([^*?$\n\r] )\* )?");
string exampleText1 = @"**** PLEASE NOTE *** Testing, *nuts*, **please note..., test
**Test text (10)";
string value = _boldRegex.Replace(exampleText1, m => 
    m.Groups[1].Success ? String.Format("<bold>{0}</bold>", m.Groups[1].Value) : "*"
);
Console.WriteLine(value);

Output

<bold> PLEASE NOTE </bold> Testing, <bold>nuts</bold>, *please note..., test
*Test text (10)
  • Related