Solved

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

Posted on 2012-03-29
5
672 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 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

810 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