[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2012-03-29
5
Medium Priority
?
686 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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

656 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