I
I
Ivan Komarov2011-06-28 10:23:43
Regular Expressions
Ivan Komarov, 2011-06-28 10:23:43

Regular expressions, maximum string length

There is a regular expression: "(\d+\.)+", which describes strings like 111.223.123.12.
How can I add a check that the total length of the string, say, does not exceed twenty characters.

Thank you.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
B
bioroot, 2011-06-28
@bioroot

Is there any good reason not to test the condition outside of the regular expression? I can't think of an adequate way to do this right off the bat without look-behinds. Actually, they are scary.
Could you please describe the problem in its entirety? From experience, when someone asks me “how to build a regular expression” in 90% of cases, the initial statement of the problem helps to find a more adequate solution, although often through a different regular expression.

N
nes, 2011-06-28
@nes

So
(\d+\.){20}

@
@antoo, 2011-06-28
_

(\d+\.){1,20}
but like this?

A
Anatoly, 2011-06-28
@taliban

Is this your IP?

@
@kuber, 2011-06-28
_

Your string 111.223.123.12 looks a lot like an ip address. It is he? Or just an example?

A
Alexey Shein, 2011-06-28
@conf

Why does it have to be done regularly? Can't you just check the length of a string without a regular expression?

M
Mithgol, 2011-07-01
@Mithgol

^(?=.{0,20}$)(\d+\.)+$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question