La expresión regular no funciona en .net

I'm using the following expression.

\W[A-C]{3}

The objective is to match 3 characters of anything between A and C that don't have any characters before them. So with input "ABC" it matches but "DABC" does not.

When i try this expression using various online regex tools (eg. http://gskinner.com/RegExr/), it works perfectly. When i try to use it in an asp.net RegularExpressionValidator or with the RegEx class, it never matches anything.

I've tried various different methods of not allowing a character before the match. eg.

[^\w] and [^a-zA-Z0-9]

all work in the online tools, but not in .net.

This test fails, but i'm not sure why?

[Test]
public void RegExWorks()
{
    var regex = new Regex("\\W[A-C]{3}");
    Match match = regex.Match("ABC");
    Assert.IsTrue(match.Success);
}

preguntado el 10 de marzo de 12 a las 05:03

5 Respuestas

Qué tal algo como esto:

^[A-C]{3}

It is simple, but seems to fit what you are asking, and I tested it in rubular.com and .NET

respondido 10 mar '12, 05:03

Yeah, your right it does work. But oddly enough it doesn't work when i try it on gskinner.com/RegExr - i'm not sure which one of 3 the answers is the best? They all work - Robbie

This is pretty standard reg ex, so I am not sure why it does not work there. If you go to the reg ex site, that is... - justin pihony

Problem is that you exigir there to be a \W character. Use alteration to fix that, or a lookbehind to make sure there are no invalid characters.

Modificación:

(?:\W|^)[A-C]{3}

But I'd prefer a negative lookbehind:

(?<!\w)[A-C]{3}

\b (as in gymbralls answer) is short for (?<!\w)(?=\w)|(?<=\w)(?!\w), which in this case would just mean (?<!\w), thus being equivalent.

Also, in C# you can use the @ quoting so you don't have to double escape things, eg:

var regex = new Regex(@"(?<!\w)[A-C]{3}");

respondido 10 mar '12, 05:03

You should consider trying:

[Test]
public void RegExWorks()
{
   var regex = new Regex("\\b[A-C]{3}");
   Match match = regex.Match("ABC");
   Assert.IsTrue(match.Success); 
}

El \\b matches a word boundary, which means it will match "ABC" as well as " ABC" and "$ABC". Using \\W requires there to be a non-word character, which doesn't sound like it is what you want.

Let me know if I'm missing something.

respondido 10 mar '12, 05:03

sorry, i wasn't clear in the question, i wanted to ensure that no characters at all are before the match. I figured \w covered what i need. I think the negative lookbehind in the first answer gets me what i need. I wouldn't want it to match "$ABC". - Robbie

@Robbie, the lookbehind in my answer se match that. Do you just want ^[A-C]{3}$? What characters are allowed before ABC? If you only want to allow white space you could use (?<!\S)[A-C]{3}. - Qimpuesto

Yeah, i think the ^ is what i want... i dont want any chars before the match... it should only match things at the start of the input. I think its what Justin has said in his answer - Robbie

Got it. Based on the question when you mentioned not wanting it to match 'DABC' it sounded like you wanted it to match those three characters only even if they had some other character before them. We might want to edit the question to make it clearer. Either way, I'm glad you found the solution you were looking for. - Gymbrall

I'm not sure i understand the ambiguity in the question? I said "3 characters of anything between A and C that don't have any characters before them". Do you mean i should have said "anything" instead of "any characters"? - Robbie

It is simple like "[A-C]{3}" this

respondido 10 mar '12, 06:03

that would not ensure matches were at the start of the input which is what i need. - Robbie

OK so you can try following Expression

"[A-C][A-C]{2}"

respondido 10 mar '12, 08:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.