Regular Expression Help

Drew Tomlinson drew at mykitchentable.net
Mon Dec 1 06:13:44 PST 2008


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



More information about the freebsd-questions mailing list