Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ptheywood/8dbb44f1a0c63f29e0fff6852dfa52f9 to your computer and use it in GitHub Desktop.
Save ptheywood/8dbb44f1a0c63f29e0fff6852dfa52f9 to your computer and use it in GitHub Desktop.

Regular expression to validate 1-4 comma separated c++ decimal values

([+-]?((([1-9][0-9]*|0))|((([0-9]+[eE][+-]?[0-9]+)|((([0-9]+\.)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?))[Ff]?)))(([ ]*,[ ]*)[+-]?(([1-9][0-9]*)|((([0-9]+[eE][+-]?[0-9]+)|((([0-9]+\.)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?))[Ff]?))){0,3}

For regex engines othern than XSD schema, wrap with ^...$ to match the full string

i.e.

^([+-]?((([1-9][0-9]*|0))|((([0-9]+[eE][+-]?[0-9]+)|((([0-9]+\.)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?))[Ff]?)))(([ ]*,[ ]*)[+-]?(([1-9][0-9]*)|((([0-9]+[eE][+-]?[0-9]+)|((([0-9]+\.)|([0-9]*\.[0-9]+))([eE][+-]?[0-9]+)?))[Ff]?))){0,3}$

Explanation

(                                           // integer or c++ float
    [+-]?                                   // sign (optional)
    (                                       
        (([1-9][0-9]*)|0)                   // integer, no leading , but can be 0
        |                                   // or
        (                                   // unsigned float with suffix
            (                               // unsigned float without suffix
                ([0-9]+[eE][+-]?[0-9]+)     // digit-sequence exponent
                |                           // or
                (                           
                    (
                        ([0-9]+\.)          // digit-sequence .
                        |                   // or
                        ([0-9]*\.[0-9]+)    // digit-sequence(optional) . digit-sequence
                    )
                    ([eE][+-]?[0-9]+)?      // exponent (optional)
                )
            )                               
            [Ff]?                           // suffix (optional)
        )
    )
)
(                                           // 0, 1, 2, or 3 comma preceded numeric values
    ([ ]*,[ ]*)                             // spaces(optional) , spaces(optional)
    [+-]?                                   // sign (optional)
    (
        (([1-9][0-9]*)|0)                   // integer, no leading , but can be 0
        |                                   // or
        (                                   // unsigned float with suffix
            (                               // unsigned float without suffix
                ([0-9]+[eE][+-]?[0-9]+)     // digit-sequence exponent
                |                           // or
                (
                    (
                        ([0-9]+\.)          // digit-sequence .
                        |                   // or
                        ([0-9]*\.[0-9]+)    // digit-sequence(optional) . digit-sequence
                    )
                    ([eE][+-]?[0-9]+)?      // exponent (optional)
                )
            )
            [Ff]?                           // suffix (optional)
        )
    )
){0,3}                                      // repeat 0 to 3 times

Alternate, much much longer version.

((([-+]?)([1-9][0-9]*))(([ ]*,[ ]*)(([-+]?)([1-9][0-9]*))){0,3})|((([-+])?((([0-9]+)([eE][-+]?[0-9]+)([fF]?))|(([0-9]+)(\.)([eE][-+]?[0-9]+)?([fF]?))|(([0-9]+)?(\.)([-+]?[0-9]+)([eE][-+]?[0-9]+)?([fF]?))))(([ ]*,[ ]*)((([-+])?((([0-9]+)([eE][-+]?[0-9]+)([fF]?))|(([0-9]+)(\.)([eE][-+]?[0-9]+)?([fF]?))|(([0-9]+)?(\.)([-+]?[0-9]+)([eE][-+]?[0-9]+)?([fF]?)))))){0,3})

Tests

This set is intentionally not full (all to all) coverage but instead demonstrates each additional part with previously tested parts. I.e. non-exhasutive

Matched Lines

// sign integer
0
1
10
+1
-1

// sign digit-sequence exponent suffix(optional)
1e2
11e22
1e2f
1E2F
1e+2
1e-2
+1e2
-1e2

// sign digit-sequence . exponent(optional) suffix(optional)   
1.
10.
1.f
1.F
+1.
+1.f
-1.
-1.f
1.e2
1.e22
1.e2f
1.E2F
1.e+2
1.e-2
+1.e2
-1.e2

// digit-sequence(optional) . digit-sequence exponent(optional) suffix(optional)   
.1
.12
.1f
-.1
+.1
.1e2
1.2
11.22
1.2f
1.2F
+1.2
-1.2
1.2e2
1.2e22
1.2e2f
1.2E2F
1.2e+2
1.2e-2
+1.2e2
-1.2e2


// Lists
1, 2
+1, -2
1, 2, 3
1, 2, 3, 4
1   ,      2 ,    3 ,4
1e2, .1e2, 1.2e3
+1e2, +.1e2, +1.2e3
-1e2, -.1e2, -1.2e3
1e+2, .1e+2, 1.2e+3
1e-2, .1e-2, 1.2e-3
1e2f, 1E2f, 1e2F, 1E2f


// Unmatched
1f
10f
08
008
+1f
-1f
1, 2,
1, 2, 3,
1, 2, 3, 4,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment