Solved

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

Posted on 2012-04-11
5
483 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

863 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

28 Experts available now in Live!

Get 1:1 Help Now