Regex vereinfachen?

Bash, Shell, PHP, Python, Perl, CGI
User avatar
Joe User
Project Manager
Project Manager
Posts: 11616
Joined: 2003-02-27 01:00
Location: Hamburg

Regex vereinfachen?

Post by Joe User » 2005-05-28 12:49

Moin,

ich bin mir sicher, dass sich folgende Regex noch verfeinern lassen, stehe aber seit Tagen irgendwie auf dem Schlauch :-/

Code: Select all

for file in `find . -type f -name "*.php"`; do sed 
-e "s@([[:space:]]*global[[:space:]]*)HTTP(_[A-Z]*)_FILES,[[:space:]]*@1@g" 
-e "s@([[:space:]]*global[[:space:]]*)HTTP(_[A-Z]*)_VARS,[[:space:]]*@1@g" 
-e "s@([[:space:]]*global[[:space:]]*.*),[[:space:]]*HTTP(_[A-Z]*)_FILES@1@g" 
-e "s@([[:space:]]*global[[:space:]]*.*),[[:space:]]*HTTP(_[A-Z]*)_VARS@1@g" 
-e "s@([[:space:]]*global[[:space:]]*.*)HTTP(_[A-Z]*)_FILES@1@g" 
-e "s@([[:space:]]*global[[:space:]]*.*)HTTP(_[A-Z]*)_VARS@1@g" 
-e "s@HTTP(_[A-Z]*)_FILES@_FILES@g" 
-e "s@HTTP(_[A-Z]*)_VARS@1@g" 
-i ${file};
done
Danke,
Joe User
PayPal.Me/JoeUserFreeBSD Remote Installation
Wings for LifeWings for Life World Run

„If there’s more than one possible outcome of a job or task, and one
of those outcomes will result in disaster or an undesirable consequence,
then somebody will do it that way.“ -- Edward Aloysius Murphy Jr.

thorsten
RSAC
Posts: 732
Joined: 2003-02-01 13:14
Location: Fuldatal

Re: Regex vereinfachen?

Post by thorsten » 2005-05-30 22:39

Ich habe leider keine passenden Zeilen parat aber du kannst es sehr wahrscheinlich folgendermaßen ein wenig vereinfachen:

Code: Select all

for file in `find . -type f -name "*.php"`; do sed -r 
-e "s@([[:space:]]*global[[:space:]]*)HTTP(_[A-Z]*)_FILES|VARS,[[:space:]]*@1@g" 
-e "s@([[:space:]]*global[[:space:]]*.*),[[:space:]]*HTTP(_[A-Z]*)_FILES|VARS@1@g" 
-e "s@([[:space:]]*global[[:space:]]*.*)HTTP(_[A-Z]*)_FILES|VARS@1@g" 
-e "s@HTTP(_[A-Z]*)_FILES@_FILES@g" 
-e "s@HTTP(_[A-Z]*)_VARS@1@g" 
-i ${file};
done
Achte auf das -r im sed Aufruf.

HTH
Thorsten

User avatar
Joe User
Project Manager
Project Manager
Posts: 11616
Joined: 2003-02-27 01:00
Location: Hamburg

Re: Regex vereinfachen?

Post by Joe User » 2005-05-31 10:12

Moin,

ich bin mitlerweile bei folgender Variante gelandet, welche zwar relativ langsam, dafür aber zuverlässig arbeitet:

Code: Select all

for file in `find . -type f -name "*.php"`; do 
sed 's/(^[[:space:]]*global[[:space:]].*)[,][[:space:]]*$HTTP_[A-Z]*_(VARS|FILES)/1/g' -i ${file} &&
sed 's/(^[[:space:]]*global[[:space:]].*)$HTTP_[A-Z]*_(VARS|FILES)([,][[:space:]]*)*/1/g' -i ${file} &&
sed 's/(^[[:space:]]*global[[:space:]].*)[,][[:space:]]*[;]$/1;/g' -i ${file} &&
sed '/^[[:space:]]*global[[:space:]]([[:space:]]*[,]*)*[;]$/d' -i ${file} &&
sed 's/HTTP(_[A-Z]*)_VARS/1/g' -i ${file} &&
sed 's/HTTP_[A-Z]*_FILES/_FILES/g' -i ${file};
done
Deine Variante werde ich heute/morgen testen, danke.

Gruss,
Joe User
PayPal.Me/JoeUserFreeBSD Remote Installation
Wings for LifeWings for Life World Run

„If there’s more than one possible outcome of a job or task, and one
of those outcomes will result in disaster or an undesirable consequence,
then somebody will do it that way.“ -- Edward Aloysius Murphy Jr.