A
A
Andrei2013-04-27 13:20:25
.NET
Andrei, 2013-04-27 13:20:25

Regular expressions and numbered backreferences

Good afternoon.

Regular expressions have such a thing as numbered backreferences.
For example, there is a date
26-4-03 or 26/04/03 or 26.4.3

Using numbered backreferences, this can be written like this:
\b\d{1,2}(\/|\.|\-)\d{ 1,2}\1(\d{1,2))\b

And how can you write a working version of something like this expression?
\b(\d{1,2})(\/|\.|\-)\1\2\1\b

Answer the question

In order to leave comments, you need to log in

4 answer(s)
U
UZER2006, 2013-04-27
@ngelik

ngelik , specifically with backlinks the way you want - you can't.
Are you making things difficult? For simple date parsing, it makes no sense to build complex regular expressions.
Check out this post. You can DEFINEdo something similar with .

V
Vyacheslav Slinko, 2013-04-27
@KeepYourMind

Your regular expression looks quite working regexr.com?34mm4
What's wrong with it?

U
UZER2006, 2013-04-27
@UZER2006

Backreferences substitute the found value, not the pattern. Maybe you are wrong about this?

\b(\d{1,2})(\/|\.|\-)\1\2\1\b

It will not work in this form. Under this expression, only lines where all 3 numbers match are suitable.

P
Placido, 2013-05-24
@Placido

I don't know if there is such a thing in .NET, but in PCRE you can use references like (?n), where n is the number of the subpattern:

\b(\d{1,2})(\/|\.|-)(?1)\2(?1)\b

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question