| 1 | package Regexp::Pattern::Float; | ||
| 2 | |||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 4 | 1 | our $DATE = '2020-05-27'; # DATE | |
| 5 | 1 | our $DIST = 'Regexp-Pattern-Float'; # DIST | |
| 6 | 1 | our $VERSION = '0.001'; # VERSION | |
| 7 | |||
| 8 | 1 | ▶ | use 5.010001; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | #use utf8; | ||
| 12 | |||
| 13 | 1 | our %RE; | |
| 14 | |||
| 15 | $RE{float_decimal} = { | ||
| 16 | 1 | summary => 'Floating number (decimal form, e.g. +12, -12.3, .4, -5.)', | |
| 17 | pat => qr/[+-]?(?:[0-9]+(?:\.[0-9]*)?|[0-9]*\.[0-9]+)/, | ||
| 18 | examples => [ | ||
| 19 | {str=>'', anchor=>1, matches=>0}, | ||
| 20 | |||
| 21 | {str=>'123', anchor=>1, matches=>1}, | ||
| 22 | {str=>'+123', anchor=>1, matches=>1}, | ||
| 23 | {str=>'-123', anchor=>1, matches=>1}, | ||
| 24 | |||
| 25 | {str=>'123.', anchor=>1, matches=>1}, | ||
| 26 | {str=>'+123.', anchor=>1, matches=>1}, | ||
| 27 | {str=>'-123.', anchor=>1, matches=>1}, | ||
| 28 | |||
| 29 | {str=>'123.0', anchor=>1, matches=>1}, | ||
| 30 | {str=>'+123.0', anchor=>1, matches=>1}, | ||
| 31 | {str=>'-123.0', anchor=>1, matches=>1}, | ||
| 32 | |||
| 33 | {str=>'123.0456', anchor=>1, matches=>1}, | ||
| 34 | {str=>'+123.0456', anchor=>1, matches=>1}, | ||
| 35 | {str=>'-123.0456', anchor=>1, matches=>1}, | ||
| 36 | |||
| 37 | {str=>'.5', anchor=>1, matches=>1}, | ||
| 38 | {str=>'+.5', anchor=>1, matches=>1}, | ||
| 39 | {str=>'-.5', anchor=>1, matches=>1}, | ||
| 40 | |||
| 41 | {str=>'.', anchor=>1, matches=>0}, | ||
| 42 | {str=>'+.', anchor=>1, matches=>0}, | ||
| 43 | {str=>'-.', anchor=>1, matches=>0}, | ||
| 44 | |||
| 45 | {str=>'1e1', anchor=>1, matches=>0, summary=>'Exponent form'}, | ||
| 46 | {str=>'Inf', anchor=>1, matches=>0, summary=>'infinity'}, | ||
| 47 | {str=>'NaN', anchor=>1, matches=>0, summary=>'nan'}, | ||
| 48 | |||
| 49 | {str=>'abc', anchor=>1, matches=>0}, | ||
| 50 | ], | ||
| 51 | }; | ||
| 52 | |||
| 53 | $RE{ufloat_decimal} = { | ||
| 54 | 1 | summary => 'Unsigned floating number (decimal form, e.g. 12, +12.3, .4, 5.)', | |
| 55 | pat => qr/[+]?(?:[0-9]+(?:\.[0-9]*)?|[0-9]*\.[0-9]+)/, | ||
| 56 | examples => [ | ||
| 57 | {str=>'', anchor=>1, matches=>0}, | ||
| 58 | |||
| 59 | {str=>'123', anchor=>1, matches=>1}, | ||
| 60 | {str=>'+123', anchor=>1, matches=>1}, | ||
| 61 | {str=>'-123', anchor=>1, matches=>0}, | ||
| 62 | |||
| 63 | {str=>'123.', anchor=>1, matches=>1}, | ||
| 64 | {str=>'+123.', anchor=>1, matches=>1}, | ||
| 65 | {str=>'-123.', anchor=>1, matches=>0}, | ||
| 66 | |||
| 67 | {str=>'123.0', anchor=>1, matches=>1}, | ||
| 68 | {str=>'+123.0', anchor=>1, matches=>1}, | ||
| 69 | {str=>'-123.0', anchor=>1, matches=>0}, | ||
| 70 | |||
| 71 | {str=>'123.0456', anchor=>1, matches=>1}, | ||
| 72 | {str=>'+123.0456', anchor=>1, matches=>1}, | ||
| 73 | {str=>'-123.0456', anchor=>1, matches=>0}, | ||
| 74 | |||
| 75 | {str=>'.5', anchor=>1, matches=>1}, | ||
| 76 | {str=>'+.5', anchor=>1, matches=>1}, | ||
| 77 | {str=>'-.5', anchor=>1, matches=>0}, | ||
| 78 | |||
| 79 | {str=>'.', anchor=>1, matches=>0}, | ||
| 80 | {str=>'+.', anchor=>1, matches=>0}, | ||
| 81 | {str=>'-.', anchor=>1, matches=>0}, | ||
| 82 | |||
| 83 | {str=>'1e1', anchor=>1, matches=>0, summary=>'Exponent form'}, | ||
| 84 | {str=>'Inf', anchor=>1, matches=>0, summary=>'infinity'}, | ||
| 85 | {str=>'NaN', anchor=>1, matches=>0, summary=>'nan'}, | ||
| 86 | ], | ||
| 87 | }; | ||
| 88 | |||
| 89 | $RE{float_exp} = { | ||
| 90 | 1 | summary => 'Floating number (exponent form, e.g. 1.2e+3, -1.2e-3)', | |
| 91 | pat => qr/[+-]?(?:[0-9]+(?:\.[0-9]*)?|[0-9]*\.[0-9]+)(?:[Ee][+-]?[0-9])/, | ||
| 92 | examples => [ | ||
| 93 | {str=>'', anchor=>1, matches=>0}, | ||
| 94 | |||
| 95 | {str=>'123', anchor=>1, matches=>0, summary=>'Decimal form'}, | ||
| 96 | {str=>'+123', anchor=>1, matches=>0, summary=>'Decimal form'}, | ||
| 97 | {str=>'-123', anchor=>1, matches=>0, summary=>'Decimal form'}, | ||
| 98 | |||
| 99 | {str=>'123e1', anchor=>1, matches=>1}, | ||
| 100 | {str=>'12.3E2', anchor=>1, matches=>1}, | ||
| 101 | {str=>'-123.e+3', anchor=>1, matches=>1}, | ||
| 102 | {str=>'+.5e-3', anchor=>1, matches=>1}, | ||
| 103 | |||
| 104 | {str=>'Inf', anchor=>1, matches=>0, summary=>'infinity'}, | ||
| 105 | {str=>'NaN', anchor=>1, matches=>0, summary=>'nan'}, | ||
| 106 | ], | ||
| 107 | }; | ||
| 108 | |||
| 109 | $RE{float_decimal_or_exp} = { | ||
| 110 | 1 | summary => 'Floating number (decimal or exponent form)', | |
| 111 | pat => qr/[+-]?(?:[0-9]+(?:\.[0-9]*)?|[0-9]*\.[0-9]+)(?:[Ee][+-]?[0-9])?/, | ||
| 112 | examples => [ | ||
| 113 | {str=>'', anchor=>1, matches=>0}, | ||
| 114 | |||
| 115 | {str=>'123', anchor=>1, matches=>1}, | ||
| 116 | {str=>'+123.', anchor=>1, matches=>1}, | ||
| 117 | {str=>'+12.3', anchor=>1, matches=>1}, | ||
| 118 | {str=>'-.123', anchor=>1, matches=>1}, | ||
| 119 | |||
| 120 | {str=>'123e1', anchor=>1, matches=>1}, | ||
| 121 | {str=>'123.e2', anchor=>1, matches=>1}, | ||
| 122 | {str=>'-1.23E+3', anchor=>1, matches=>1}, | ||
| 123 | {str=>'+.5e-3', anchor=>1, matches=>1}, | ||
| 124 | |||
| 125 | {str=>'Inf', anchor=>1, matches=>0, summary=>'infinity'}, | ||
| 126 | {str=>'NaN', anchor=>1, matches=>0, summary=>'nan'}, | ||
| 127 | ], | ||
| 128 | }; | ||
| 129 | |||
| 130 | $RE{float_inf} = { | ||
| 131 | 1 | summary => 'Infinity', | |
| 132 | pat => qr/[+-]?(?:infinity|inf)/i, | ||
| 133 | examples => [ | ||
| 134 | {str=>'', anchor=>1, matches=>0}, | ||
| 135 | {str=>'123', anchor=>1, matches=>0}, | ||
| 136 | {str=>'Inf', anchor=>1, matches=>1}, | ||
| 137 | {str=>'-Inf', anchor=>1, matches=>1}, | ||
| 138 | {str=>'+infinity', anchor=>1, matches=>1}, | ||
| 139 | {str=>'infini', anchor=>1, matches=>0}, | ||
| 140 | {str=>'NaN', anchor=>1, matches=>0, summary=>'nan'}, | ||
| 141 | ], | ||
| 142 | }; | ||
| 143 | |||
| 144 | $RE{float_nan} = { | ||
| 145 | 1 | summary => 'NaN', | |
| 146 | pat => qr/nan/i, | ||
| 147 | examples => [ | ||
| 148 | {str=>'', anchor=>1, matches=>0}, | ||
| 149 | {str=>'123', anchor=>1, matches=>0}, | ||
| 150 | {str=>'Inf', anchor=>1, matches=>0, summary=>'infinity'}, | ||
| 151 | {str=>'NaN', anchor=>1, matches=>1, summary=>'nan'}, | ||
| 152 | {str=>'nan', anchor=>1, matches=>1, summary=>'nan'}, | ||
| 153 | {str=>'+NaN', anchor=>1, matches=>0, summary=>'nan does not recognize sign'}, | ||
| 154 | {str=>'-NaN', anchor=>1, matches=>0, summary=>'nan does not recognize sign'}, | ||
| 155 | ], | ||
| 156 | }; | ||
| 157 | |||
| 158 | $RE{float} = { | ||
| 159 | 1 | summary => 'Floating number (decimal or exponent form, or Inf/NaN)', | |
| 160 | pat => qr/(?:[+-]?(?:[0-9]+(?:\.[0-9]*)?|[0-9]*\.[0-9]+)(?:[Ee][+-]?[0-9])?|[+-]?(?:infinity|inf)|nan)/i, # XXX only set inf/nan parts as case-insensitive | ||
| 161 | examples => [ | ||
| 162 | {str=>'', anchor=>1, matches=>0}, | ||
| 163 | |||
| 164 | # decimal | ||
| 165 | {str=>'123', anchor=>1, matches=>1}, | ||
| 166 | {str=>'+123.', anchor=>1, matches=>1}, | ||
| 167 | {str=>'+12.3', anchor=>1, matches=>1}, | ||
| 168 | {str=>'-.123', anchor=>1, matches=>1}, | ||
| 169 | |||
| 170 | # exp | ||
| 171 | {str=>'123e1', anchor=>1, matches=>1}, | ||
| 172 | {str=>'123.e2', anchor=>1, matches=>1}, | ||
| 173 | {str=>'-1.23E+3', anchor=>1, matches=>1}, | ||
| 174 | {str=>'+.5e-3', anchor=>1, matches=>1}, | ||
| 175 | |||
| 176 | # inf | ||
| 177 | {str=>'Inf', anchor=>1, matches=>1}, | ||
| 178 | {str=>'-Inf', anchor=>1, matches=>1}, | ||
| 179 | {str=>'+infinity', anchor=>1, matches=>1}, | ||
| 180 | {str=>'infini', anchor=>1, matches=>0}, | ||
| 181 | |||
| 182 | # nan | ||
| 183 | {str=>'NaN', anchor=>1, matches=>1, summary=>'nan'}, | ||
| 184 | {str=>'nan', anchor=>1, matches=>1, summary=>'nan'}, | ||
| 185 | {str=>'+NaN', anchor=>1, matches=>0, summary=>'nan does not recognize sign'}, | ||
| 186 | {str=>'-NaN', anchor=>1, matches=>0, summary=>'nan does not recognize sign'}, | ||
| 187 | |||
| 188 | {str=>'abc', anchor=>1, matches=>0}, | ||
| 189 | ], | ||
| 190 | }; | ||
| 191 | |||
| 192 | $RE{ufloat} = { | ||
| 193 | 1 | summary => 'Unsigned floating number (decimal or exponent form, or Inf/NaN)', | |
| 194 | pat => qr/(?:[+]?(?:[0-9]+(?:\.[0-9]*)?|[0-9]*\.[0-9]+)(?:[Ee][+-]?[0-9])?|[+]?(?:infinity|inf)|nan)/i, # XXX only set inf/nan parts as case-insensitive | ||
| 195 | examples => [ | ||
| 196 | {str=>'', anchor=>1, matches=>0}, | ||
| 197 | |||
| 198 | # decimal | ||
| 199 | {str=>'123', anchor=>1, matches=>1}, | ||
| 200 | {str=>'+123.', anchor=>1, matches=>1}, | ||
| 201 | {str=>'+12.3', anchor=>1, matches=>1}, | ||
| 202 | {str=>'-.123', anchor=>1, matches=>0}, | ||
| 203 | |||
| 204 | # exp | ||
| 205 | {str=>'123e1', anchor=>1, matches=>1}, | ||
| 206 | {str=>'123.e2', anchor=>1, matches=>1}, | ||
| 207 | {str=>'-1.23E+3', anchor=>1, matches=>0}, | ||
| 208 | {str=>'+.5e-3', anchor=>1, matches=>1}, | ||
| 209 | |||
| 210 | # inf | ||
| 211 | {str=>'Inf', anchor=>1, matches=>1}, | ||
| 212 | {str=>'-Inf', anchor=>1, matches=>0}, | ||
| 213 | {str=>'+infinity', anchor=>1, matches=>1}, | ||
| 214 | {str=>'infini', anchor=>1, matches=>0}, | ||
| 215 | |||
| 216 | # nan | ||
| 217 | {str=>'NaN', anchor=>1, matches=>1, summary=>'nan'}, | ||
| 218 | {str=>'nan', anchor=>1, matches=>1, summary=>'nan'}, | ||
| 219 | {str=>'+NaN', anchor=>1, matches=>0, summary=>'nan does not recognize sign'}, | ||
| 220 | {str=>'-NaN', anchor=>1, matches=>0, summary=>'nan does not recognize sign'}, | ||
| 221 | |||
| 222 | {str=>'abc', anchor=>1, matches=>0}, | ||
| 223 | ], | ||
| 224 | }; | ||
| 225 | |||
| 226 | 1 | 1; | |
| 227 | # ABSTRACT: Regexp patterns related to floating (decimal) numbers | ||
| 228 | |||
| 229 | __END__ | ||