Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>First Answer:</strong></p> <p>This matches <code>#,###,##0.00</code>:</p> <pre><code>^[+-]?[0-9]{1,3}(?:\,?[0-9]{3})*(?:\.[0-9]{2})?$ </code></pre> <p>And this matches <code>#.###.##0,00</code>:</p> <pre><code>^[+-]?[0-9]{1,3}(?:\.?[0-9]{3})*(?:\,[0-9]{2})?$ </code></pre> <p>Joining the two (there are smarter/shorter ways to write it, but it works):</p> <pre><code>(?:^[+-]?[0-9]{1,3}(?:\,?[0-9]{3})*(?:\.[0-9]{2})?$) |(?:^[+-]?[0-9]{1,3}(?:\.?[0-9]{3})*(?:\,[0-9]{2})?$) </code></pre> <p>You can also, add a capturing group to the last comma (or dot) to check which one was used.</p> <hr> <p><strong>Second Answer:</strong></p> <p>As pointed by <strong>Alan M</strong>, my previous solution could fail to reject a value like <code>11,111111.00</code> where a comma is missing, but the other isn't. After some tests I reached the following regex that avoids this problem:</p> <pre><code>^[+-]?[0-9]{1,3} (?:(?&lt;comma&gt;\,?)[0-9]{3})? (?:\k&lt;comma&gt;[0-9]{3})* (?:\.[0-9]{2})?$ </code></pre> <p>This deserves some explanation:</p> <ul> <li><p><code>^[+-]?[0-9]{1,3}</code> matches the first (1 to 3) digits;</p></li> <li><p><code>(?:(?&lt;comma&gt;\,?)[0-9]{3})?</code> matches on optional comma followed by more 3 digits, and captures the comma (or the inexistence of one) in a group called 'comma';</p></li> <li><p><code>(?:\k&lt;comma&gt;[0-9]{3})*</code> matches zero-to-any repetitions of the comma used before (if any) followed by 3 digits;</p></li> <li><p><code>(?:\.[0-9]{2})?$</code> matches optional "cents" at the end of the string.</p></li> </ul> <p>Of course, that will only cover <code>#,###,##0.00</code> (not <code>#.###.##0,00</code>), but you can always join the regexes like I did above.</p> <hr> <p><strong>Final Answer:</strong></p> <p>Now, a complete solution. Indentations and line breaks are there for readability only.</p> <pre><code>^[+-]?[0-9]{1,3} (?: (?:\,[0-9]{3})* (?:.[0-9]{2})? | (?:\.[0-9]{3})* (?:\,[0-9]{2})? | [0-9]* (?:[\.\,][0-9]{2})? )$ </code></pre> <p>And this variation captures the separators used:</p> <pre><code>^[+-]?[0-9]{1,3} (?: (?:(?&lt;thousand&gt;\,)[0-9]{3})* (?:(?&lt;decimal&gt;\.)[0-9]{2})? | (?:(?&lt;thousand&gt;\.)[0-9]{3})* (?:(?&lt;decimal&gt;\,)[0-9]{2})? | [0-9]* (?:(?&lt;decimal&gt;[\.\,])[0-9]{2})? )$ </code></pre> <hr> <p><strong>edit 1</strong>: "cents" are now optional; <strong>edit 2</strong>: text added; <strong>edit 3</strong>: second solution added; <strong>edit 4</strong>: complete solution added; <strong>edit 5</strong>: headings added; <strong>edit 6</strong>: capturing added; <strong>edit 7</strong>: last answer broke in two versions;</p>
    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