0

I'm trying to show formatted label in Xamarin forms (never mind if you are not familiar, the issue is not dependant on this). Given a string which can contain any character and <b></b> tag, I need to create FormattedString object, which contains spans of text. For instance

Lorem ipsum dolor sit amet, consectetur &lt;b&gt;adipiscing elit&lt;/b&gt;. Integer imperdiet massa accumsan turpis ullamcorper tempor. &lt;b&gt;Cras eget erat quis mi sollicitudin vehicula.&lt;/b&gt; Sed ac risus mattis.

This text should be transformed into these spans

  1. Lorem ipsum dolor sit amet, consectetur

  2. adipiscing elit

  3. . Integer imperdiet massa accumsan turpis ullamcorper tempor.

  4. Cras eget erat quis mi sollicitudin vehicula.

  5. Sed ac risus mattis.

I'm using following regular expression

 private static Regex _formatterRegex = new Regex(@"(?<text>[^<>\\/]+)|(?<bold><b>(?<boldBody>[^<>\\/]*)<\\/b>)");

So I match either <b>SOME TEXT</b> or arbitrary text. The problem here is that 'b' of <b></b> tags is also matched. Have a look into matches

enter image description here

I need somehow to match arbitrary text only if it's not inside <b></b> tags, so my match collection will not contain b-s of the html tags. Any ideas?

2
  • Can you give me a link? I also wouldn't like to use whole HTML parsing lib for parsing just one single tag. In any case I want to solve this with regex, so I need a regex solution. Commented Feb 1, 2016 at 13:59
  • 1
    I suggest reading about The Greatest Regex Trick Ever. Commented Feb 1, 2016 at 14:24

1 Answer 1

0

Here's how I'd approach it:

var text = "Lorem ipsum dolor sit amet, consectetur &lt;b&gt;adipiscing elit&lt;/b&gt;. Integer imperdiet massa accumsan turpis ullamcorper tempor. &lt;b&gt;Cras eget erat quis mi sollicitudin vehicula.&lt;/b&gt; Sed ac risus mattis.";

var regex = new Regex("(?<text>.+?)(?:&.*?;.*?;|$)" );

var matches = regex.Matches( text );

foreach(Match m in matches ){
    Console.WriteLine( "|" + m.Groups["text"].Value.Trim() + "|" );
}

Produces:

|Lorem ipsum dolor sit amet, consectetur|
|adipiscing elit|
|. Integer imperdiet massa accumsan turpis ullamcorper tempor.|
|Cras eget erat quis mi sollicitudin vehicula.|
|Sed ac risus mattis.|
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.