hankknight
asked on
ASP.NET / VB / REGEX: Automatically Make Headings Bold
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
-------------------------- ---------- ---------- ---
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.
- 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>")
--------------------------
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.
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 ?/?>":
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")
ASKER
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 + "• ")
input = RegularExpressions.Regex.Replace(input, VbNewLine, VbNewLine +"<br />")
return input
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.
ASKER
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.
"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 + "• ")
input = RegularExpressions.Regex.Replace(input, VbNewLine, VbNewLine +"<br />")
return input
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! I have posted a related question here:
https://www.experts-exchange.com/questions/27657662/ASP-NET-VB-REGEX-Automatically-Make-Headings-Bold-Including-First-Line.html
https://www.experts-exchange.com/questions/27657662/ASP-NET-VB-REGEX-Automatically-Make-Headings-Bold-Including-First-Line.html
Open in new window