jspicher
jspicher

Reputation: 113

What is this regex supposed to do? ^[\d\D]{1,}$

I'm not exactly a pro when it comes to regex and I have a PHP script that runs things through this regex:

^[\d\D]{1,}$

What does this supposed to do, it seems that it matches everything?

Upvotes: 2

Views: 124

Answers (4)

MikeM
MikeM

Reputation: 13631

^[\d\D]{1,}$ will match a string which contains one or more {1,} of any digit \d or non-digit \D character including newline characters.

In contrast ^.+$ will match a string containing one or more of any character except newlines. If the singleline modifier was added to the regex, i.e. /^.+$/s then the . would also match any character including newlines.

[\d\D] is equivalent to using . in singleline mode, although more commonly [\s\S] is used with the same result.

+ is equivalent to {1,}.

The regex will match the whole of any string that contains at least one character of any kind.

Upvotes: 1

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

You are right. In fact anything that is at least one character long. But in a kind of overcomplicated and pointless way. [\d\D] is equivalent to . and {1,} is equivalent to +

Upvotes: 0

anubhava
anubhava

Reputation: 785058

In short all that regex is doing is this:

^.+$

Which means match any character (digits OR non-digits) of 1 or greater length.

Upvotes: 1

Knaģis
Knaģis

Reputation: 21485

  • \d matches any digit
  • \D matches any non-digit.
  • [\d\D] matches all digits and non-digits.
  • {1,} asks for the match in [] to be repeated at least 1 time (with no upper limit).

So it matches everything with at least 1 character in it.

Reference: http://www.regular-expressions.info/reference.html

Upvotes: 2

Related Questions