Java Regex match zero times or once
<p>I was playing around with Regex in Java, and I wanted to match a string which has zero or one occurrence of a dot <code>.</code>. So I want any strings with a single dot or no dot to return true, and a string with two or more dots to return false.</p>
<p>According to the Java documentation:</p>
<blockquote>
<p>X?       X, once or not at all</p>
</blockquote>
<p>So I have the following:</p>
<pre><code>String str = &quot;abc.def&quot;;
System.out.println(str.matches(&quot;.?&quot;));
</code></pre>
<p>I was expecting <code>str</code> to match for <code>.</code> zero or one time, but it prints <code>false</code>.</p>
<p>Did I misinterpret the meaning of <code>?</code>? What can I do to make it match zero or one time only?</p>