tags:

views:

119

answers:

5

I need to validate input: valid variants are either number or empty string. What is the correspondent regular expression?

String pattern = "\d+|<what shoudl be here?>";

UPD: dont suggest "\d*" please, I'm just curious how to tell "empty string" in regexp.

A: 

There shouldn't be anything wrong with just "\d+|"

umop
That's not correct. An empty regex will match the empty string but also (multiple times) in every non-empty string, namely at each position before or after a character.
Tim Pietzcker
@Tim: but if this is Java `matches`, then the pattern will be matched against the _entire_ input string anyway. That is, in Java, `"blah".matches("")` is `false`.
polygenelubricants
Correct. To clarify, wrapping in ^ and $ will make sure that you're talking about the whole string.
umop
+6  A: 
/^\d*$/

Matches 0 or more digits with nothing before or after.

Explanation:

The '^' means start of line. '$' means end of line. '*' matches 0 or more occurences. So the pattern matches an entire line with 0 or more digits.

KaptajnKold
+2  A: 

In this particular case, ^\d*$ would work, but generally speaking, to match pattern or an empty string, you can use:

^$|pattern

Explanation

  • ^ and $ are the beginning and end of the string anchors respectively.
  • | is used to denote alternates, e.g. this|that.

References

Related questions


Note on multiline mode

In the so-called multiline mode (Pattern.MULTILINE/(?m) in Java), the ^ and $ match the beginning and end of the line instead. The anchors for the beginning and end of the string are now \A and \Z respectively.

If you're in multiline mode, then the empty string is matched by \A\Z instead. ^$ would match an empty line within the string.


Examples

Here are some examples to illustrate the above points:

String numbers = "012345";

System.out.println(numbers.replaceAll(".", "<$0>"));
// <0><1><2><3><4><5>

System.out.println(numbers.replaceAll("^.", "<$0>"));
// <0>12345

System.out.println(numbers.replaceAll(".$", "<$0>"));
// 01234<5>

numbers = "012\n345\n678";
System.out.println(numbers.replaceAll("^.", "<$0>"));       
// <0>12
// 345
// 678

System.out.println(numbers.replaceAll("(?m)^.", "<$0>"));       
// <0>12
// <3>45
// <6>78

System.out.println(numbers.replaceAll("(?m).\\Z", "<$0>"));     
// 012
// 345
// 67<8>

Note on Java matches

In Java, matches attempts to match a pattern against the entire string.

This is true for String.matches, Pattern.matches and Matcher.matches.

This means that sometimes, anchors can be omitted for Java matches when they're otherwise necessary for other flavors and/or other Java regex methods.

Related questions

polygenelubricants
+3  A: 

To explicitly match the empty string, use \A\Z.

You can also often see ^$ which works fine unless the option is set to allow the ^ and $ anchors to match not only at the start or end of the string but also at the start/end of each line. If your input can never contain newlines, then of course ^$ is perfectly OK.

Some regex flavors don't support \A and \Z anchors (especially JavaScript).

If you want to allow "empty" as in "nothing or only whitespace", then go for \A\s*\Z or ^\s*$.

Tim Pietzcker
+1  A: 

Just as a funny solution, you can do:

\d+|\d{0}

A digit, zero times. Yes, it does work.

unbeli