tags:

views:

33

answers:

2

What I tried and failed...

match.Success is always wrong:

Regex pattern = new Regex(@"^-*Text\\r\\n"); 

sample text:

"--My Text" newline+return
bla
A: 

There's whitespace after the second " in the sample text. Try inserting a space after the second " in your pattern. Also, your question looks a lot like this one:

http://stackoverflow.com/questions/3307479/regex-with-can-be-numerous-and-must-newline-after-the-string

Phil
sorry that space was a mistakesamples that can occur:-----My Text\\r\\n--My Text\\r\\nMy Text\\r\\n
msfanboy
@msfanboy, do you mean \\r\\n or \r\n?
Phil
works Regex pattern = new Regex(@"-*MyText\n");I tried \\ to escape the \ but oddly that did not work??
msfanboy
No because you have a `@` at the beginning. You don't need to escape the backslash if you use the @ symbol - it's handy for regexes and path strings.
Phil
ah stupid me lol totally forgot why to use the @P
msfanboy
A: 

Technically, not all new lines will be a full \r\n - it's OS dependent. So you might want to adjust for that. However, assuming that the only environment you need to worry about is your own, and \r\n fits for that, then the following should do the trick (if I understand what you're looking for):

Regex pattern = new Regex(@"^-.*Text\r\n");

This is stating the following: 1. Line must start with one - character 2. After the - character, anything can come (including more - characters) as long as it stays on the same line and is followed by the string Text and then followed by \r\n

If you don't mean to require the string to be Text, then it can be even simpler. The follow accepts anything as long as it starts with a - and ends with a new line:

Regex pattern = new Regex(@"^-.*\r\n");
JGB146