crenshaw-dev
crenshaw-dev

Reputation: 8412

How can I check some arbitrary regular expression and then its length?

I would like it all to be in one regular expression, because the (to me) black box tool validating the input accepts only one regex, and I would rather not introduce my own external logic to double-check.

Specifically, I am trying to modify dperini's URL regex to make sure the input is a valid URL and then make sure its length is no greater than a certain number of characters, so it fits in the database column.

i.e., I want to combine regex_check('<insert dperini's magic>); and regex_check('^.{0,250}'); into one regular expression.

Upvotes: 0

Views: 37

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15311

You can use a zero length lookahead with your regex to have it peek ahead and check the string length. The match will only succeed if the lookahead is true as well as the rest of the pattern:

^(?=^.{0,250}$)...

where the ... is the other pattern.

Upvotes: 2

Related Questions