Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Regexp substitution using variables

by kcott (Archbishop)
on Nov 25, 2020 at 22:11 UTC ( [id://11124230]=note: print w/replies, xml ) Need Help??


in reply to Regexp substitution using variables

G'day MikeTaylor,

Or perhaps something like this, if I could only find the right class name:

my $re = new Regexp($pattern); my $value = $re->substitute($value, replacement, $flags);

About 15 or 20 years ago, I read a book by Damian Conway called "Object Oriented Perl". In it, he shows the creation of blessed objects using various things including regular expressions: your post reminded me of this.

I don't own the book. If you can get a copy (you possibly already own one) it's certainly worth reading even though it's now quite old. If you follow the link I provided, you'll see a free PDF copy is offered; however, it looks like you need to "add to cart" which probably also means you have to "create an account" — I didn't follow through on this.

Here's a very quick-and-dirty implementation of a class which blesses regular expressions.

package Regex; use strict; use warnings; sub new { my ($class, $pattern, $flags) = @_; my $flag_part = defined $flags ? "(?$flags)" : ''; my $re_part = "\Q$pattern"; return bless qr{$flag_part$re_part}, $class; } sub match { my ($self, $str) = @_; return $str =~ $self ? 'YES' : 'NO'; } sub replace { my ($self, $str, $new) = @_; $str =~ s/$self/$new/; return $str; } 1;

If you want to use something like this in production code, it'll need a lot more work. What I've provided is only intended to demonstrate the basic principles involved. The book would probably provide a lot more information; but I don't remember details of something I read about two decades ago.

Here's a test of that module:

#!/usr/bin/env perl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use Regex; my $pat = 'b'; my $case_sens_re_obj = Regex::->new($pat); my $case_insens_re_obj = Regex::->new($pat, 'i'); my $test_string = 'ABC'; print 'case_sens_re_obj match: ', $case_sens_re_obj->match($test_string), "\n"; print 'case_insens_re_obj match: ', $case_insens_re_obj->match($test_string), "\n"; print 'case_sens_re_obj substition: ', $case_sens_re_obj->replace($test_string, '_'), "\n"; print 'case_insens_re_obj substition: ', $case_insens_re_obj->replace($test_string, '_'), "\n";

Output:

case_sens_re_obj match: NO case_insens_re_obj match: YES case_sens_re_obj substition: ABC case_insens_re_obj substition: A_C

Unrelated but important: Please avoid indirect object syntax; e.g. new Regexp($pattern). See "perlobj: Indirect Object Syntax" for a discussion of problems with this syntax. The above example would be much better as Regexp::->new($pattern) — "perlobj: Invoking Class Methods" explains that.

P.S. When checking links prior to posting, I noticed "PDF for FREE" has been replaced with the text, "pBook + PDF". I don't know what that means and whether the PDF is still free or not (there would have only been a matter of minutes between finding the link in the first place and checking I had correctly included it in my post).

— Ken

Replies are listed 'Best First'.
Re^2: Regexp substitution using variables
by MikeTaylor (Acolyte) on Nov 25, 2020 at 22:47 UTC
    Thanks, Ken, this is helpful. Point taken on Class->new, too.

    The problem with this class, like the solutions above that use s/(?$flags:$pattern)/$replacement/ directly, is that it doesn't handle back-references. (That's true even with the \Q removed from the definition of $re_part in the class constructor.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124230]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found