1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
NAME
Regexp::Pattern - Convention/framework for modules that contain
collection of regexes
SPECIFICATION VERSION
0.2
VERSION
This document describes version 0.2.14 of Regexp::Pattern (from Perl
distribution Regexp-Pattern), released on 2020-04-01.
SYNOPSIS
Subroutine interface:
use Regexp::Pattern; # exports re()
my $re = re('YouTube::video_id');
say "ID does not look like a YouTube video ID" unless $id =~ /\A$re\z/;
# a dynamic pattern (generated on-demand) with generator arguments
my $re2 = re('Example::re3', {variant=>"B"});
Hash interface (a la Regexp::Common but simpler with regular/non-magical
hash that is only 1-level deep):
use Regexp::Pattern 'YouTube::video_id';
say "ID does not look like a YouTube video ID"
unless $id =~ /\A$RE{video_id}\z/;
# more complex example
use Regexp::Pattern (
're', # we still want the re() function
'Foo::bar' => (-as => 'qux'), # the pattern will be in your $RE{qux}
'YouTube::*', # wildcard import
'Example::re3' => (variant => 'B'), # supply generator arguments
'JSON::*' => (-prefix => 'json_'), # add prefix
'License::*' => (
# filtering options
-has_tag => 'family:cc', # only select patterns that have this tag
-lacks_tag => 'type:unversioned', # only select patterns that do not have this tag
-has_tag_matching => qr/^type:/, # only select patterns that have at least a tag matching this regex
-lacks_tag_matching => qr/^type:/, # only select patterns that do not have any tags matching this regex
# other options
-prefix => 'pat_', # add prefix
-suffix => '_license', # add suffix
),
);
DESCRIPTION
Regexp::Pattern is a convention for organizing reusable regexp patterns
in modules, as well as framework to provide convenience in using those
patterns in your program.
Structure of an example Regexp::Pattern::* module
package Regexp::Pattern::Example;
our %RE = (
# the minimum spec
re1 => { pat => qr/\d{3}-\d{3}/ },
# more complete spec
re2 => {
summary => 'This is regexp for blah', # plaintext
description => <<'_',
A longer description in *Markdown* format.
_
pat => qr/\d{3}-\d{3}(?:-\d{5})?/,
tags => ['A','B'],
examples => [
# examples can be tested using 'test-regexp-pattern' script
# (distributed in Test-Regexp-Pattern distribution). examples can
# also be rendered in your POD using
# Pod::Weaver::Plugin::Regexp::Pattern.
{
str => '123-456',
matches => 1,
},
{
summary => 'Another example that matches',
str => '123-456-78901',
matches => 1,
},
{
summary => 'An example that does not match',
str => '123456',
matches => 0,
},
{
summary => 'An example that does not get tested',
str => '123456',
},
{
summary => 'Another example that does not get tested nor rendered to POD',
str => '234567',
matches => 0,
test => 0,
doc => 0,
},
],
},
# dynamic (regexp generator)
re3 => {
summary => 'This is a regexp for blah blah',
description => <<'_',
...
_
gen => sub {
my %args = @_;
my $variant = $args{variant} || 'A';
if ($variant eq 'A') {
return qr/\d{3}-\d{3}/;
} else { # B
return qr/\d{3}-\d{2}-\d{5}/;
}
},
gen_args => {
variant => {
summary => 'Choose variant',
schema => ['str*', in=>['A','B']],
default => 'A',
req => 1,
},
},
tags => ['B','C'],
examples => [
{
summary => 'An example that matches',
gen_args => {variant=>'A'},
str => '123-456',
matches => 1,
},
{
summary => "An example that doesn't match",
gen_args => {variant=>'B'},
str => '123-456',
matches => 0,
},
],
},
re4 => {
summary => 'This is a regexp that does capturing',
# it is recommended that your pattern does not capture, unless
# necessary. capturing pattern should tag with 'capturing' to let
# users/tools know.
tags => ['capturing'],
pat => qr/(\d{3})-(\d{3})/,
examples => [
{str=>'123-456', matches=>[123, 456]},
{str=>'foo-bar', matches=>[]},
],
},
re5 => {
summary => 'This is another regexp that is anchored and does (named) capturing',
# it is recommended that your pattern is not anchored for more
# reusability, unless necessary. anchored pattern should tag with
# 'anchored' to let users/tools know.
tags => ['capturing', 'anchored'],
pat => qr/^(?<cap1>\d{3})-(?<cap2>\d{3})/,
examples => [
{str=>'123-456', matches=>{cap1=>123, cap2=>456}},
{str=>'something 123-456', matches=>{}},
],
},
);
A Regexp::Pattern::* module must declare a package global hash variable
named %RE. Hash keys are pattern names, hash values are pattern
definitions in the form of defhashes (see DefHash).
Pattern name should be a simple identifier that matches this regexp:
"/\A[A-Za-z_][A-Za-z_0-9]*\z/". The definition for the qualified pattern
name "Foo::Bar::baz" can then be located in
%Regexp::Pattern::Foo::Bar::RE under the hash key "baz".
Pattern definition hash should at the minimum be:
{ pat => qr/.../ }
You can add more stuffs from the defhash specification, e.g. summary,
description, tags, and so on, for example (taken from
Regexp::Pattern::CPAN):
{
summary => 'PAUSE author ID, or PAUSE ID for short',
pat => qr/[A-Z][A-Z0-9]{1,8}/,
description => <<~HERE,
I'm not sure whether PAUSE allows digit for the first letter. For safety
I'm assuming no.
HERE
examples => [
{str=>'PERLANCAR', matches=>1},
{str=>'BAD ID', anchor=>1, matches=>0},
],
}
Examples. Your regexp specification can include an "examples" property
(see above for example). The value of the "examples" property is an
array, each of which should be a defhash. For each example, at the
minimum you should specify "str" (string to be matched by the regexp),
"gen_args" (hash, arguments to use when generating dynamic regexp
pattern), and "matches" (a boolean value that specifies whether the
regexp should match the string or not, or an array/hash that specifies
the captures). You can of course specify other defhash properties (e.g.
"summary", "description", etc). Other example properties might be
introduced in the future.
If you use Dist::Zilla to build your distribution, you can use the
plugin [Regexp::Pattern] to test the examples during building, and the
Pod::Weaver plugin [-Regexp::Pattern] to render the examples in your
POD.
Using a Regexp::Pattern::* module
Standalone
A Regexp::Pattern::* module can be used in a standalone way (i.e. no
need to use via the Regexp::Pattern framework), as it simply contains
data that can be grabbed using a normal means, e.g.:
use Regexp::Pattern::Example;
say "Input does not match blah"
unless $input =~ /\A$Regexp::Pattern::Example::RE{re1}{pat}\z/;
Via Regexp::Pattern, sub interface
Regexp::Pattern (this module) also provides "re()" function to help
retrieve the regexp pattern. See "re" for more details.
Via Regexp::Pattern, hash interface
Additionally, Regexp::Pattern (since v0.2.0) lets you import regexp
patterns into your %RE package hash variable, a la Regexp::Common (but
simpler because the hash is just a regular hash, only 1-level deep, and
not magical).
To import, you specify qualified pattern names as the import arguments:
use Regexp::Pattern 'Q::pat1', 'Q::pat2', ...;
Each qualified pattern name can optionally be followed by a list of
name-value pairs. A pair name can be an option name (which is dash
followed by a word, e.g. "-as", "-prefix") or a generator argument name
for dynamic pattern.
Wildcard import. Instead of a qualified pattern name, you can use
'Module::SubModule::*' wildcard syntax to import all patterns from a
pattern module.
Importing into a different name. You can add the import option "-as" to
import into a different name, for example:
use Regexp::Pattern 'YouTube::video_id' => (-as => 'yt_id');
Prefix and suffix. You can also add a prefix and/or suffix to the
imported name:
use Regexp::Pattern 'Example::*' => (-prefix => 'example_');
use Regexp::Pattern 'Example::*' => (-suffix => '_sample');
Filtering. When wildcard-importing, you can select the patterns you want
using a combination of these options: "-has_tag" (only select patterns
that have a specified tag), "-lacks_tag" (only select patterns that do
not have a specified tag), "-has_tag_matching" (only select patterns
that have at least one tag matching specified regex pattern),
"-lacks_tag_matching" (only select patterns that do not have any tags
matching specified regex pattern).
Recommendations for writing the regex patterns
* Regexp pattern should in general be written as a "qr//" literal
instead of string
That is:
pat => qr/foo[abc]+/,
is preferred over:
pat => 'foo[abc]+',
Using a string literal is less desirable because of lack of
compile-time checking. An exception to this rule is when you want to
delay regex compilation for some reason, e.g. you want your user to
compile the patterns themselves using different regex engine (see
"re::engine::*" modules on CPAN).
* Regexp pattern should not be anchored (unless really necessary)
That is:
pat => qr/foo/,
is preferred over:
pat => qr/^foo/, # or qr/foo$/, or qr/\Afoo\z/
Adding anchors limits the reusability of the pattern. When composing
pattern, user can add anchors herself if needed.
When you define an anchored pattern, adding tag "anchored" is
recommended:
tags => ['anchored'],
* Regexp pattern should not contain capture groups (unless really
necessary)
Adding capture groups limits the reusability of the pattern because
it can affect the groups of the composed pattern. When composing
pattern, user can add captures herself if needed.
When you define a capturing pattern, adding tag "capturing" is
recommended:
tags => ['capturing'],
FUNCTIONS
re
Exported by default. Get a regexp pattern by name from a
"Regexp::Pattern::*" module.
Usage:
re($name[, \%args ]) => $re
$name is *MODULE_NAME::PATTERN_NAME* where *MODULE_NAME* is name of a
"Regexp::Pattern::*" module without the "Regexp::Pattern::" prefix and
*PATTERN_NAME* is a key to the %RE package global hash in the module. A
dynamic pattern can accept arguments for its generator, and you can pass
it as hashref in the second argument of "re()".
Anchoring. You can also put "-anchor => 1" in %args. This will
conveniently wraps the regex inside "qr/\A(?:...)\z/". To only add left
anchoring, specify "-anchor => 'left'" ("qr/\A(?:...)/". To only add
right anchoring, specify "-anchor => 'right'" ("qr/(?:...)\z/".
Die when pattern by name $name cannot be found (either the module cannot
be loaded or the pattern with that name is not found in the module).
FAQ
My pattern is not anchored, but what if I want to test the anchored version?
You can add "anchor=>1" or "gen_args=>{-anchor=>1}" in the example, for
example:
{
summary => 'PAUSE author ID, or PAUSE ID for short',
pat => qr/[A-Z][A-Z0-9]{1,8}/,
description => <<~HERE,
I'm not sure whether PAUSE allows digit for the first letter. For safety
I'm assuming no.
HERE
examples => [
{str=>'PERLANCAR', matches=>1},
{str=>'BAD ID', anchor=>1, matches=>0, summary=>"Contains whitespace"},
{str=>'NAMETOOLONG', gen_args=>{-anchor=>1}, matches=>0, summary=>"Too long"},
],
}
HOMEPAGE
Please visit the project's homepage at
<https://metacpan.org/release/Regexp-Pattern>.
SOURCE
Source repository is at
<https://github.com/perlancar/perl-Regexp-Pattern>.
BUGS
Please report any bugs or feature requests on the bugtracker website
<https://rt.cpan.org/Public/Dist/Display.html?Name=Regexp-Pattern>
When submitting a bug or request, please include a test-file or a patch
to an existing test-file that illustrates the bug or desired feature.
SEE ALSO
Regexp::Common. Regexp::Pattern is an alternative to Regexp::Common.
Regexp::Pattern offers simplicity and lower startup overhead. Instead of
a magic hash, you retrieve available regexes from normal data structure
or via the provided "re()" function. Regexp::Pattern also provides a
hash interface, albeit the hash is not magic.
Regexp::Common::RegexpPattern, a bridge module to use patterns in
"Regexp::Pattern::*" modules via Regexp::Common.
Regexp::Pattern::RegexpCommon, a bridge module to use patterns in
"Regexp::Common::*" modules via Regexp::Pattern.
App::RegexpPatternUtils
If you use Dist::Zilla: Dist::Zilla::Plugin::Regexp::Pattern,
Pod::Weaver::Plugin::Regexp::Pattern,
Dist::Zilla::Plugin::AddModule::RegexpCommon::FromRegexpPattern,
Dist::Zilla::Plugin::AddModule::RegexpPattern::FromRegexpCommon.
Test::Regexp::Pattern and test-regexp-pattern.
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2020, 2019, 2018, 2016 by
perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
|