Solved

ASP.NET / VB / REGEX: Bold first part with colon

Posted on 2012-03-29
5
670 Views
Last Modified: 2012-03-30
Using REGEX with ASP.NET / VB, how can I format this:

Any text that is immediately after a line break (or at the very beginning) should be bold if there is a colon in the first sentence, from the beginning to the colon.

This  should be bold: this should not be bold -- this should not be bold: it it comes after a bold part.
This should also be bold: it is at the beginning.
Nothing in this line should be bold.  This is a colon: however there is a period before the colon.

My code below does not work.
input = RegularExpressions.Regex.Replace(input, VbNewLine + "\W:", VbNewLine + "<strong>$1</strong>")

Open in new window

0
Comment
Question by:hankknight
  • 3
5 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 37783261
Pattern: ^([\w\d\s]*:)
Code, must use multiline:
Input = System.Text.RegularExpressions.Regex.Replace(Input, "^([\w\d\s]*:)", "<strong>$1</strong>", System.Text.RegularExpressions.RegexOptions.Multiline)

Open in new window

0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 37783416
Use a literal space instead of the "\s" in PaulHews', I think.
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 350 total points
ID: 37783499
Good catch.  Otherwise it will include line break as a space character.

Input = System.Text.RegularExpressions.Regex.Replace(Input, "^([\w\d ]*:)", "<strong>$1</strong>", System.Text.RegularExpressions.RegexOptions.Multiline)

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 37784081
Thanks.

What is the purpose of
System.Text.RegularExpressions.
and
System.Text.RegularExpressions.RegexOptions.Multiline
?

My other expressions do not have those:
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "&#8226; ")
input = RegularExpressions.Regex.Replace(input, VbNewLine, VbNewLine +"<br />")
input = RegularExpressions.Regex.Replace(input, "(https?://\S+[^\s@,.""']+)", "<a href=""$1"">$1</a>")
input = RegularExpressions.Regex.Replace(input, "([^\s@,.""']+\@\S+\.[^\s@,.""']+)", "<a href=""mailto:$1"">$1</a>")
input = RegularExpressions.Regex.Replace(input, "(?<!\S)(\www\.\S+[^\s@,.""']+)", "<a href=""http://$1"">$1</a>")
input = RegularExpressions.Regex.Replace(input, "  ", " &nbsp;")

Open in new window

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37784184
If you have imported the namespace then you don't need System.Text in front.   (The full namespace for the Regex class is System.Text.RegularExpressions.  Your code above calls functions in the Regex class using only the RegularExpressions part of the namespace.)   I had not imported the namespace when I tested the code.

System.Text.RegularExpressions.RegexOptions.Multiline is an option.  This is required because I use a caret ^ in the expression to indicate the beginning of each line.  If I did not specify "multiline" option, then the caret would instead match the beginning of the entire input string, and so would only replace the first instance with the bold text.

Regular Expression Options
http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now