go to previous page   go to home page   go to next page

Answer:

[0-9]*       matches strings of any length (including zero), composed entirely of digits.

[^0-9]*     matches strings of any length (including zero), composed entirely of NON-digits.


Predefined Character Classes

Regular Expression
(Delimit with quotes)
String
(Delimit with quotes)

Some character classes are so useful that short-cuts have been defined for them.

Digits: \d means the same as [0-9].

Whitespace: \s matches space, tab, newline, vertical tab, formfeed, and carriage return. This is useful because it matches more than just space, and prevents the problems of spaces in an RE.

Non-Whitespace: \S matches everything but whitespace.

To match more than one of these, use *, +, and {n,m}. For example \d* matches digit strings of any length. Notice that the * applies to the entire \d, not just the d character. The table shows several additional shortcuts.

Short CutEquivalent Description
\d [0-9] A digit
\D [^0-9] A non-digit
\s [ \t\n\x0B\f\r] A whitespace character
\S [^\s] A non-whitespace character
\w [a-zA-Z_0-9] A word character
\W [^\w] A non-word character

QUESTION 2:

Write a regular expression that matches strings consisting of one or more digits, but not starting with digit 0, surrounded by any amount of whitespace on either or both sides. For example:

" 123"    "1023 "    "4"    "  7"    "2210"

But not:

"0"   "0123  "    "  0  "