Solved

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

Posted on 2012-03-29
5
680 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 75

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

688 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