Re: pattern matching



On 7/17/06, Ryan Dillinger <puppetmyst@xxxxxxxxxxx> wrote:

Now I believe I understand it up until the the last part \1.

/([a-zA-z]{3})\s*\1/

That's a backreference; it matches if the corresponding part of the
string is equal to what's in memory one at the time of the match.
Memory one holds the part of the string matched by the part of the
pattern inside the first pair of parentheses; in this case, that's the
string of exactly three letters that matched earlier. This pattern
will match strings like these:

Paris in the the Spring.
barbarian
instantaneous
somewhat hated
microphone one

There's some confusion about what \1 and $1 mean, since they seem to
be two different names for the same string. Actually they're two very
different things: $1 is a (read-only) variable, holding a string from
a previous, completed, pattern match. But \1 is referring back to the
memory created in the current pattern match, which hasn't yet
completed. (You didn't ask, but that's why backreferences should be
found only in patterns, such as on the "pattern side" of the s///
command; on the "string side", since the pattern match is completed,
\1 isn't correct.)

You can find more about backreferences in the regular expression
manpages, such as perlre and perlrequick.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
.



Relevant Pages

  • Re: Fast string operations
    ... Looping: I thought looping over arrays in managed code was "slow" ... array handling and such. ... The problem with TrimHelper is that it always returns a new string instance. ... The customer perceives this as a memory leak. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Discovering variable types...
    ... >- but I suppose MS expect us to use wrappers ... memory allocations for your variables from disk as well. ... >They most certainly are of fixed size, changing the size of a String ... >>me to keep buffer size and current postion right in the memory block. ...
    (comp.lang.pascal.delphi.misc)
  • Re: Optimize
    ... if you use it like me to get 'the next string ptr' in addition. ... | |mov ebx eax;dw aligned strings are faster ... | Would it be enough to touch each 16th bytes in the 64K memory area ... Cache-line size is fixed, my AMD got 64 bytes per line. ...
    (alt.lang.asm)
  • Memory Question
    ... I have wrote a helper to my program which monitors object count, memory utilization and a threshold of 10% to determine how many objects are sticking around and how many are being garbage collected. ... occured right before the original String count). ... def print_threshold_breakers hsh1, hsh2, threshold ... putsf 'Building mem usage', mem_usage ...
    (comp.lang.ruby)
  • Re: Theodore Adorno, a prophet of data systems design
    ... > memory leaks, overruns etc., and I can even prove this without knowing ... possible to design a language in which buffer overruns are impossible. ... C makes the allocation of fixed-size arrays possible because there are some ... design and implement your own string model. ...
    (comp.programming)