Solved

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

Posted on 2012-04-11
5
487 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 Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
vb.net winforms sizing/resolution? 4 42
Finding Events logs for IIS website that restarts 2 14
System.net.Sockets Error 5 28
VB.NET Textbox input validation 4 40
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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

828 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