GetBoldWords

  • Thread starter Thread starter Aaron
  • Start date Start date
A

Aaron

This example that Mun give me (see below) works fine if theres only one set of
bold tags <b>ddf</b> but it would fail if there are more than one
input = "<b>ddf</b><b>dddfs</b>"
output = "ddf</b><b>dddfs"

is there a way to save each word in bold into an array?

Thanks,
aaron
-------------------
Here's my original post
I need a function in c# that finds and outputs word in bold.


string input = "<b>Hello my name</b> is John"

public string GetBoldWords()
{
//the function
return s // s = "Hello my name"
}

thanks
-------------------------------------------------
You could use the regular expression - <b>(.*)</b>.

public string GetBoldWords(string somestring)
{
string foundMatch = string.Empty;
Regex RegExObj= new Regex("<b>(.*)</b>", RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Match MatchObj =
RegExObj.Match(somestring);
if (MatchObj.Success) foundMatch = MatchObj.Groups[1].Value;
return foundMatch;
}


Hope this helps,

Mun
 
You need to to use a non-greedy regular expression (see example below and
note the ? immediately after the .* that forces it to take the first </b> it
can find after the <b>; otherwise it would grab the last one. You can then
go through the matches for the list of bold words and/or add all these to an
array:

Dim foundMatch As String = ""
Dim RegExObj As Regex = New Regex("<b>.*?</b>", RegexOptions.IgnoreCase)
For Each m As Match In RegExObj.Matches("Hello my name is <b>John</b>
and I'm a <b>codeaholic</b>")
For Each c As Capture In m.Captures
Response.Write(Server.HtmlEncode(Mid(c.Value, 4, Len(c.Value) - 7))
& "<br>")
Next
Next



Aaron said:
This example that Mun give me (see below) works fine if theres only one set of
bold tags <b>ddf</b> but it would fail if there are more than one
input = "<b>ddf</b><b>dddfs</b>"
output = "ddf</b><b>dddfs"

is there a way to save each word in bold into an array?

Thanks,
aaron
-------------------
Here's my original post
I need a function in c# that finds and outputs word in bold.


string input = "<b>Hello my name</b> is John"

public string GetBoldWords()
{
//the function
return s // s = "Hello my name"
}

thanks
-------------------------------------------------
You could use the regular expression - <b>(.*)</b>.

public string GetBoldWords(string somestring)
{
string foundMatch = string.Empty;
Regex RegExObj= new Regex("<b>(.*)</b>", RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Match MatchObj =
RegExObj.Match(somestring);
if (MatchObj.Success) foundMatch = MatchObj.Groups[1].Value;
return foundMatch;
}


Hope this helps,

Mun
 
Back
Top