Click here to Skip to main content
12,688,898 members (29,996 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#
Ive got an exp

regex2 = @"(?<=<td><a href=""\/en\/was-passiert-heute\/uebersicht\/datum\_)\d+\-\d+\-\d+";

I get an error that unrecognized escape sequence. Whats wrong?
Posted 8-Jan-13 4:38am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The problem is here:
@"(?<=<td><a href=""\/en\/was-passiert-heute\/uebersicht\/datum\_)\d+\-\d+\-\d+"
- So you either need the backslash, than you have to double it: \\_
- Or you wanted to escape the underscore, but that's not needed, just remove the backslash before the underscore
Andreas Gieriet 8-Jan-13 15:07pm
My 5!
According to Character Escapes, redundant escapes are allowed. Why \_ is causing an error "escapes" my knowledge ;-) It appears to be a bug to me...
In any case, since the \_ is redundant (i.e. no need to escape underscores), it's easy to get the pattern working.
Zoltán Zörgő 8-Jan-13 15:17pm
Yes, might be a bug, but who knows :) Thank you anyway!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

A backslash is used to start an escape sequence. In your case it looks as if you are trying to escape the forward slash which is being used in URLs. There is no need to do that and also not for the underscore as Zoltán already pointed out. You do need to escape the quotes though, but not because of the regular expression rather because they are occurring in an already quoted string:

    regex2 = @"(?<=<td><a href=\"/en/was-passiert-heute/uebersicht/datum_)\d+-\d+-\d+";

A minus should also need no escaping as long as it's not used in a character class of the regular expression. Look here for some more information: what-literal-characters-should-be-escaped-in-a-regex[^]


— Manfred
Marcus Kramer 8-Jan-13 11:12am
Andreas Gieriet 8-Jan-13 14:51pm
It's a verbatim string (@"..."), so the original "-escaping was correct: in a verbatim string, you must escape " by "".
See also Escaping in C#: characters, strings, string formats, keywords, identifiers.
Manfred R. Bihy 8-Jan-13 15:36pm
That's what you may do. I always use a backslash to escape quotes in a string literal. OP tagged the question as C#, so thatis what I went by. I've never heard of a double quote doing the same in C#. Enlighten me if you will? :)
Manfred R. Bihy 8-Jan-13 15:37pm
Forget what I just said I somehow missed the @!

Shame on me, sorry!
Andreas Gieriet 8-Jan-13 16:58pm
Hello Manfred,
no problem! Sometimes I'm also so much focussed on something else so that I overlook the obvious ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170113.4 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100