?
Solved

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

Posted on 2012-03-29
5
Medium Priority
?
681 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 600 total points
ID: 37783416
Use a literal space instead of the "\s" in PaulHews', I think.
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 1400 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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.
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…
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 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