Solved

ASP.NET / VB / REGEX: Convert dashes to bullets if at beginning of line

Posted on 2012-04-11
5
480 Views
Last Modified: 2012-06-27
I want to replace a dash with a bullet if it is at the beginning of a line.

The problem with my code is that it does not work on the first line.  It works for all lines except the first line.

This:
- Hello world
- This is a test
- Item XYZ

Should become this:
• Hello world
• This is a test
• Item XYZ

 
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "• ")

Open in new window

0
Comment
Question by:hankknight
  • 3
5 Comments
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 166 total points
ID: 37835408
Its bacause you have "VbNewLine" in the expression.
0
 
LVL 6

Assisted Solution

by:yawkey13
yawkey13 earned 168 total points
ID: 37835431
Regex.Replace(input, "^- ", "• ") will get the first one.  The easiest would be to just call both.

input = RegularExpressions.Regex.Replace(input, "^- ", "• ") ;
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "• ");
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 37835450
The reason why it does not work in the first line is becuase the first does not contain any carriage return or new line. Your code replaces only the character that is VbNewLine with "- ". As a work around append try the below code:

Approach one:
input = "•" + RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "• ")

Open in new window


Approach two:
input = string.Format("{0}{1} {2},"VbNewLine, "-", input)
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "• ")
0
 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 37835453
Typo erro, approach two should be like this:

Approach two:
input = string.Format("{0}{1} {2}"VbNewLine, "-", input)
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "• ")
0
 
LVL 8

Accepted Solution

by:
PagodNaUtak earned 166 total points
ID: 37835457
Typo error, approach two should be like this:

Approach two:
input = string.Format("{0}{1} {2}", VbNewLine, "-", input)
input = RegularExpressions.Regex.Replace(input, VbNewLine + "- ", VbNewLine + "• ")
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now