tags:

views:

55

answers:

4

How would a regex look like when I search for this string:

before CAN be many comment lines --------"Encrypted" after must come a newline.

this does not seem to work:

Regex pattern = new Regex(@"^[-]*$[Encrypted][\n]");

what do I wrong?

A: 

Try it without the square brackets and move the dollar $ to the end of the pattern. Ie:

Regex pattern = new Regex(@"^-*Encrypted$"); 

The square brackets is like an Or statement. So [Encrypted] is the same as saying: 'E' or 'n' or 'c' or 'r'.... or 'd'.

The dollar symbol matches the end of the string.

Phil
I have this: --Encrypted newline....your regex does not work, match.Success is false
Sanju
@Sanju, Please provide some sample text.
Phil
+1  A: 

The pattern you're searching for is not entirely clear to me, nor are the rest of the contents you're searching in, but if you're really just looking for "Encrypted" directly followed by only a newline then this is all you need to do:

Regex r = new Regex(@"Encrypted\n")

EDIT

Ok, comments seem to suggest that you're looking for zero or more occurences of "-", followed by "Encrypted", followed by newline. In that case the following will work.

Regex r = new Regex(@"-*Encrypted\n");

If there should be at least one "-" before "Encrypted", it will be

Regex r = new Regex(@"-+Encrypted\n");
Willem van Rumpt
Comment symbols then Encrypted then a newline, I think.
Phil
+1  A: 

I don't know specifically regex for c# but i thing you must not put the ^ because -'s are not at the begining of the line. And what the $ is doing in the middle.

So i would do in PCRE regex:

/-+"Encrypted" *\n/

That match one or more - followed by "Encryption" followed by 0 or more space followed by newline.

M42
A: 

@Sanju

does this work :P

Regex pattern = new Regex(@"-*Encrypted\n");

for me it did! You have to remove the "^" char

msfanboy