Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 01 Dec 2008 06:13:28 -0800
From:      Drew Tomlinson <drew@mykitchentable.net>
To:        Matthew Seaman <m.seaman@infracaninophile.co.uk>
Cc:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Regular Expression Help
Message-ID:  <4933F108.4050407@mykitchentable.net>
In-Reply-To: <493385F4.6010809@infracaninophile.co.uk>
References:  <4932CA0D.8080109@mykitchentable.net> <4932D91F.5040804@infracaninophile.co.uk> <4932E8A2.6030804@mykitchentable.net> <493385F4.6010809@infracaninophile.co.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
Matthew Seaman wrote:
> Drew Tomlinson wrote:
>> Matthew Seaman wrote:
>
>>> % perl -p -e 's/cn=([^ ,]+) ([^,]+),/cn=$2 $1,/' < foo.txt 
>
>> I still don't really understand *why* the above works but I'm trying 
>> to pick it apart now.
>
> The RE breaks down like this:
>
> /cn=([^ ,]+) ([^,]+),/
> cn=                     Match literal text 'cn='
>    (                    capture #1 begin
>     [^ ,]               Character class: anything that is not space or 
> comma
>          +              At least one of the above
>           )             end capture #1
>                         Match a literal space
>             (           capture #2 begin
>              [^,]       Character class: anything that is not a comma
>                  +      At least one of the above
>                   )     end capture #2
>                    ,    Match literal comma

Thank you for that.  You've shown me a new way to look at things.  
Instead of worrying about what I want to match, determine what marks the 
beginning and end of what I want to match and use negated character 
classes to find those end points.  I think my regex writing has just 
gotten better.  :)

Cheers,

Drew


-- 
Be a Great Magician!
Visit The Alchemist's Warehouse

http://www.alchemistswarehouse.com




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4933F108.4050407>