Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: help in understanding odd regex match

by jweed (Chaplain)
on Feb 20, 2004 at 06:34 UTC ( [id://330477]=note: print w/replies, xml ) Need Help??


in reply to help in understanding odd regex match

There are a few things that are a bit screwey with this:
  • You can easily avoid using $& here, and since it is really taxing on all other regexen, just don't. I'll point out how to do it along the way.
  • Substitution 1: s/([^ a-zA-Z0-9_=&\-])/\\$1/g;. Benefits: No $& to deal with (added parens in front to compensate), as well as stopping an useless sprintf call.
  • Substitution 2: s/([A-Za-z0-9][^&\/\.]){10,}/.*/g;. Benefits: Stops the useless and wrong (arguments that you pass a screwey) sprintf call. But see below.
  • Substitution 3: $string =~ s/([0-9]){2,}/\\d+/g;. Benefits: Again, no screwey sprintf.
There's a question about your second s/// though: Currently, you look for 10 or more pairs of one alphanumeric character and one non-special character. The reason why you have ?.*2 in your actual string is because you have an ODD number of characters. Your "pair" semantics leave one behind, therefore. We'll need more info before we can decide what you actually want here.

HTH.



Code is (almost) always untested.
http://www.justicepoetic.net/

Replies are listed 'Best First'.
Re: Re: help in understanding odd regex match
by jaco (Pilgrim) on Feb 20, 2004 at 07:08 UTC
    Thank you,

    I see the mistake i'm making now in the second s///. all better.

    Also i was unaware that using $& was any worse the $1,$2.
    Thanks for pointing that out as well.

    I do appreciate the help.

      $& slows down every regular expression in your program where $1 and friends just slow down those regular expressions that use them. Same goes for $` and $'. This is documented (albeit sparsely) in perlvar

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found