[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: Automatically Make Headings Bold

Posted on 2012-03-30
7
Medium Priority
?
480 Views
Last Modified: 2012-04-01
Using REGEX with ASP.NET / VB, how can I format plain text to make the headings bold?  Text is a heading if:
- It is 100 character or shorter than
- The first and last word begin with a capital letter
- It does not end with a period or a comma or a hyphen or a question mark or a colon
- There are one or more line breaks before it
- There are exactly two line breaks after it

input = RegularExpressions.Regex.Replace(input,  ????, "<strong>???</strong>")

Open in new window




-------------------------------------------------

Hello World

This is a test and only a test.  The heading above should be bold.

This is a Heading!

The heading above should be bold.  If it had ended with a question mark or a period or a comma or a hyphen it should not have been bold.

ALL CAPS HEADING

The heading above should be bold.
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
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37789507
Try:

input = RegularExpressions.Regex.Replace(input, "(\n)(?=.{1,100}\n)([A-Z][a-z]*(?:(?: [a-z]+)* [A-Z][a-z]*)?)(\r\n\r\n)", "$1<strong>$2</strong>$3")

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37789513
P.S.

If by "line break" you actually mean an HTML <br /> tag, then you can replace the single \n and the two \r\n with "<br ?/?>":

input = RegularExpressions.Regex.Replace(input, "(<br ?/?>)(?=.{1,100}\n)([A-Z][a-z]*(?:(?: [a-z]+)* [A-Z][a-z]*)?)(<br ?/?><br ?/?>)", "$1<strong>$2</strong>$3")

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 37792116
Thanks but it is not working for me:


input = "Hello World" + VbNewLine + VbNewLine + "This is a test and only a test.  The heading above should be bold. " + VbNewLine +VbNewLine + "This is a Heading!" + VbNewLine +VbNewLine + "The heading above should be bold.  If it had ended with a question mark or a period or a comma or a hyphen it should not have been bold." + VbNewLine +VbNewLine + "ALL CAPS HEADING" + VbNewLine + VbNewLine + "The heading above should be bold. "
input = RegularExpressions.Regex.Replace(input, "(\n)(?=.{1,100}\n)([A-Z][a-z]*(?:(?: [a-z]+)* [A-Z][a-z]*)?)(\r\n\r\n)", "$1<strong>$2</strong>$3q")
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "&#8226; ")
input = RegularExpressions.Regex.Replace(input, VbNewLine, VbNewLine +"<br />")
return input

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37792153
Because you don't have a leading line break. Your requirement said: "There are one or more line breaks before it." Your example does not match this requirement.
0
 
LVL 16

Author Comment

by:hankknight
ID: 37793241
Thanks, but I am still having trouble with it.

"The first and last word begin with a capital letter" however the code below only works if the heading is two words long.

It should make the line bold if the first and last word begin with bold letters but it should not matter if other words inside the heading do not begin with capital letters.

input = VbNewLine + VbNewLine + "Bold Heading One" + VbNewLine + VbNewLine + "This is a test and only a test.  The heading above should be bold." + VbNewLine +VbNewLine + "Heading 2 Should be Bold" + VbNewLine +VbNewLine + "The heading above should be bold.  If it had ended with a question mark or a period or a comma or a hyphen it should not have been bold." + VbNewLine +VbNewLine + "Bold Heading" + VbNewLine + VbNewLine + "The heading above should be bold. "
input = RegularExpressions.Regex.Replace(input, "(\n)(?=.{1,100}\n)([A-Z][a-z]*(?:(?: [a-z]+)* [A-Z][a-z]*)?)(\r\n\r\n)", "$1<strong>$2</strong>$3")
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "&#8226; ")
input = RegularExpressions.Regex.Replace(input, VbNewLine, VbNewLine +"<br />")
return input

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 37793272
I took the requirement, "The first and last word begin with a capital letter," as *only* the first and last letter should be capitalized. It's easy enough to fix, though.

...
input = RegularExpressions.Regex.Replace(input, "(\n)(?=.{1,100}\n)([A-Z][a-z]*(?:(?: [a-zA-Z]+)* [A-Z][a-z]*)?)(\r\n\r\n)", "$1<strong>$2</strong>$3")
...

Open in new window

0
 
LVL 16

Author Comment

by:hankknight
ID: 37793363
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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