Alberto Valero Ortega
Alberto Valero Ortega

Reputation: 47

Exclude string from regular expression in C#

I have the next re using the c# regex class:

[^\r\n]+[^,"condicionesDePago"]

searching in the next text:

"CREDITO 30 DIAS","condicionesDePago"

I want to get the left part, in this case:"CREDITO 30 DIAS" but i only get : "CREDITO 30

what im missing?

Upvotes: 0

Views: 2696

Answers (2)

dash
dash

Reputation: 91550

If you always expect the text to be the format above, then you might want to be explicit in your match:

   "(\w+ [0-9]+ \w+)"(?=,"condicionesDePago")

This should match Credit 30 Days (i.e. the pattern letters numbers letters - \w+ [0-9]* \w+) followed by condicionesDePago - (?=,"condicionesDePago") - called a Positive Lookahead.

I always use http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx to test my regular expressions, but I bet other people have their faves too.

Upvotes: 0

Ahmad Mageed
Ahmad Mageed

Reputation: 96557

It looks like you have a misunderstanding of what the character class is used for. It doesn't operate on words, but rather on individual characters. This part of your pattern, [^,"condicionesDePago"] indicates that a character should be matched provided it is not a comma, double quotes, or specified alphabet in that word.

You probably intended to use a look-ahead:

@"[^\r\n]+(?=,""condicionesDePago"")"

Alternately, this could be written as follows and won't match \n as long as RegexOptions.Singleline isn't specified (but it can match \r):

@".+?(?=,""condicionesDePago"")";

Upvotes: 2

Related Questions