Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: My regex works, but I want to make sure it's not blind luck

by BillKSmith (Prior)
on Dec 22, 2020 at 00:03 UTC ( #11125592=note: print w/replies, xml ) Need Help??


in reply to My regex works, but I want to make sure it's not blind luck

Your explanation is reasonably good. The problem with your regex is that it can make false matches. Have you considered Regexp::Common::net?
Bill
  • Comment on Re: My regex works, but I want to make sure it's not blind luck

Replies are listed 'Best First'.
Re^2: My regex works, but I want to make sure it's not blind luck
by SergioQ (Beadle) on Dec 22, 2020 at 01:09 UTC

    The problem with your regex is that it can make false matches.

    Is there a way to tell regex to work from right to left?

      Have you a test case that fails? Why do you think "right to left" will fix the failing test case?

      Note that you can reverse a string with reverse then match against the reversed string. But that's only going to help if you can identify the problem you are trying to fix, and reversing the string somehow fixes that problem.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Is there a way to tell regex to work from right to left?

      No. One of the basic principles of the regex engine is that it works from left to right (GrandFather's suggestion of reverseing the string is a workaround/hack, though I personally have never seen anyone actually do this in production). Another basic principle is that the engine will stop at the first successful match, which sometimes leads to confusion when, for example, people expect .* to match more than "" (though in your example in the root node you're using the ^ $ anchors to help with that). Combine this with the idea of backtracking (Update: which of course does work from right to left, but too much backtracking can be very inefficient) and hopefully this will lead to a better understanding :-) I very much recommend a read of perlretut, and if you want to see your regex in action, then install Regexp::Debugger and run e.g. perl -MRegexp::Debugger -e '"foo.bar" =~ /^.*(\..*)$/'

        I had a hard time thinking of an example where reversing the string before using a regex made sense. I came across this little gem..
        use strict; use warnings; sub commify { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; } foreach (qw(-1000 23000000)) { print "$_ transformed to: ",commify($_),"\n"; } __END__ -1000 transformed to: -1,000 23000000 transformed to: 23,000,000
        There are of course ways to do this with printf().
      Not exactly right to left, but this code looks for a line that ends with a period, followed by at least one character that is not a period before the end of the line or string. The period and characters that follow it are captured. If the match fails, $suffix is set to a null string. You can specify what you want in relation to the end of the string, but this is not "backwards" or right to left - this is the "rightmost" pattern that matches.

      use strict; use warnings; foreach my $test ('..', 'file.txt', 'blah.abc.txt') { my ($suffix) = $test =~ /(\.[^.]+)$/; $suffix //= ''; #suffix is null string if no match print "test=$test suffix=$suffix\n"; } __END__ test=.. suffix= test=file.txt suffix=.txt test=blah.abc.txt suffix=.txt
      There are many modules like: File::Basename.

      Update: I guess probably: my ($suffix) = $test =~ /(\.\w+)$/;
      Word characters are A-Za-z0-9_. Space and control chars are not allowed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-04-11 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?