Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I assume the language is <strong>Java</strong>. I also assume that you want to validate user input, which <strong><em>expects</em></strong> to contain <strong>only</strong> the percentage.</p> <pre><code>"^\\s*((100(\\.0*)?|\\d{1,2}(\\.\\d*)?|\\.\\d+)%?|PASSED|FAILED)\\s*$" </code></pre> <p>(In Groovy, you don't have to escape the backslash)</p> <pre><code>/^\s*((100(\.0*)?|\d{1,2}(\.\d*)?|\.\d+)%?|PASSED|FAILED)\s*$/ (for Groovy) </code></pre> <p>The regex above will <strong>only</strong> validate numbers between 0.0 to 100.0. It allows arbitrary long number of digits after decimal points, and also allows cases such as <code>.8</code> or <code>45.</code> to pass, since they can be processed. The regex also allows any amount of spaces before and after the input (but not inside).</p> <p><b>EDIT</b></p> <p>To match plain text (ASCII):</p> <pre><code>"^\\s*((100(\\.0*)?|\\d{1,2}(\\.\\d*)?|\\.\\d+)%?|[a-zA-Z.]+)\\s*$" /^\s*((100(\.0*)?|\d{1,2}(\.\d*)?|\.\d+)%?|[a-zA-Z.]+)\s*$/ (for Groovy) </code></pre> <p>This will match text that uses English alphabet character (case-insensitive), and full-period. E.g. <code>sdhSDFHS.sfSDJF.sdfSDJFI</code> will matches the above regex. Note that the current regex does not allow spaces within the text, but if you want to add more characters, add inside the square bracket (character class): <code>[a-zA-Z.]</code>. Check the <a href="http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html" rel="nofollow">documentation</a> for more details.</p> <p><strong>Testing code</strong> (for Java)</p> <pre><code>String arr[] = {"100%", " .435%", "3%", "0%", "0", ".0", "hdfjs.", " PASSED", "FAILED", "34.5 ", "sdhf.sdfdsf.fsdf ", " 100.0000 ", "-234.4", "-0.1", "34.", "100.0000001", "1000"}; for (String s: arr) System.out.println(s + " " + (s.matches("^\\s*((100(\\.0*)?|\\d{1,2}(\\.\\d*)?|\\.\\d+)%?|[a-zA-Z.]+)\\s*$") ? "matched" : "not matched")); </code></pre> <p><strong>Output</strong></p> <pre><code>100% matched .435% matched 3% matched 0% matched 0 matched .0 matched hdfjs. matched PASSED matched FAILED matched 34.5 matched sdhf.sdfdsf.fsdf matched 100.0000 matched -234.4 not matched -0.1 not matched 34. matched 100.0000001 not matched 1000 not matched </code></pre> <p><strong>EDIT2</strong></p> <p>Change in requirement as mentioned in the discussion: only match from 1.0 to 100.0 inclusive for the percentage:</p> <pre><code>/^\s*((100(\.0*)?|[1-9]\d?(\.\d*)?)%?|[a-zA-Z.]+)\s*$/ (for Groovy) </code></pre> <p>This condition is even more stricter than before, since <code>01.1</code> will not match. If you want to match this case:</p> <pre><code>/^\s*(0*((100(\.0*)?|[1-9]\d?(\.\d*)?)%?)|[a-zA-Z.]+)\s*$/ (for Groovy) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload