logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Regexp::Stringify - Stringify a Regexp object

Author

       perlancar <perlancar@cpan.org>

Bugs

       Please     report     any     bugs     or     feature     requests     on    the    bugtracker    website
       <https://rt.cpan.org/Public/Dist/Display.html?Name=Regexp-Stringify>

       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.

Functions

stringify_regexp(%args)->str
       Stringify a Regexp object.

       This routine is an alternative to Perl's default stringification of Regexp object (i.e.:"$re") and has
       some features/options, e.g.: producing regexp string that is compatible with certain perl versions.

       If given a string (or other non-Regexp object), will return it as-is.

       This function is not exported by default, but exportable.

       Arguments ('*' denotes required arguments):

       •   plver => str

           Target perl version.

           Try to produce a regexp object compatible with a certain perl version (should at least be >= 5.10).

           For  example,  in  perl  5.14  regex  stringification  changes, e.g. "qr/hlagh/i" would previously be
           stringified as "(?i-xsm:hlagh)", but now it's stringified as "(?^i:hlagh)". If  you  set  "plver"  to
           5.10  or  5.12, then this routine will still produce the former. It will also ignore regexp modifiers
           that are introduced in newer perls.

           Note that not all regexp objects are translatable to older perls, e.g. if they contain constructs not
           known to older perls like "(?^...)" before perl 5.14.

       •   regexp* => rewith_qr => bool

           If you set this to 1, then "qr/a/i" will  be  stringified  as  'qr/a/i'  instead  as  '(?^i:a)'.  The
           resulting string can then be eval-ed to recreate the Regexp object.

       Return value:  (str)

Homepage

       Please visit the project's homepage at <https://metacpan.org/release/Regexp-Stringify>.

Name

       Regexp::Stringify - Stringify a Regexp object

Source

       Source repository is at <https://github.com/perlancar/perl-Regexp-Stringify>.

Synopsis

       Assuming this runs on Perl 5.14 or newer.

        use Regexp::Stringify qw(stringify_regexp);
        $str = stringify_regexp(regexp=>qr/a/i);                       # '(^i:a)'
        $str = stringify_regexp(regexp=>qr/a/i, with_qr=>1);           # 'qr(a)i'
        $str = stringify_regexp(regexp=>qr/a/i, plver=>5.010);         # '(?:(?i-)a)'
        $str = stringify_regexp(regexp=>qr/a/ui, plver=>5.010);        # '(?:(?i-)a)'

Version

       This document describes version 0.06 of Regexp::Stringify (from Perl distribution Regexp-Stringify),
       released on 2016-10-29.

See Also