On 3/7/2011 8:13 AM, Stan Hoeppner wrote:
Noel Jones put forth on 3/7/2011 7:00 AM:
On 3/7/2011 4:47 AM, Stan Hoeppner wrote:

I was taught to always start my expressions with "/^" and end them with
"$/".  Why did Steven teach me to do this if it's not necessary?

That's good advice when you're actually matching something.

Ok, so if I'm doing what I've heard called a "fully qualified regular
expression", WRT FQrDNS matching, should I use the anchors or not?
postmap -q says these all work (the actuals with action and text that is).

/^(\d{1,3}-){3}\d{1,3}\.dynamic\.chello\.sk$/
/^(\d{1,3}\.){4}dsl\.dyn\.forthnet\.gr$/
/^(\d{1,3}-){4}adsl-dyn\.4u\.com\.gh$/
/^[\d\w]{8}\.[\w]{2}-[\d]-[\d\w]{2}\.dynamic\.ziggo\.nl$/
/^(\d{1,3}\.){4}dynamic\.snap\.net\.nz$/
/^pppoe-dyn(-\d{1,3}){4}\.kosnet\.ru$/

In these examples, you're explicitly matching something at the start and/or end of the string. Using the anchors is correct and recommended.



The special case of .* means, as you know, "anything or nothing".
There's never a case where it's necessary to explicitly match a leading
or trailing "anything or nothing".

What of the case where you want to match something in the middle of the
input string, with extra junk on both ends?

If you're looking for a string that contains foo anywhere, simply
/foo/
with no anchors.



Consider:
/^.*foo$/
   match the string beginning with anything or nothing, ending with foo.

can always be simplified to:
/foo$/
   match the string ending with foo.

This works the same without the ending $ anchor (contains foo, rather
than ends with foo), but helps the illustration.

So, in my examples above, given we're matching rDNS patterns, are the
anchors necessary, or helpful?  If not using them means "contains", then
they should still match.  What advantage is there to using the anchors
when matching rDNS patterns?  Any?

You use anchors to reduce the chance of a false positive. A side benefit is improved performance.

Any pattern that matches with the anchors will still match without the anchors, but may match additional input that you don't intend to match. In the case of the rDNS patterns, a FP is unlikely (but possible, more so with the shorter patterns).

In other cases, such as matching a sort bare domain name, a FP may be very likely without anchors.

"best practice" is to use the anchors when you can, ie. what you're matching will always be at the beginning and/or end of the input string. Never use ^.* or .*$.


  -- Noel Jones

Reply via email to