I have many PHP Files where I need to add on end "()" to every constant beginning with "L::" which don't have on end "(". It should by applied to all *.php including subfolders) :


should become:


but not touch any of them:

L::WHAT_EVER($a, $b)

Is there any sed regex command that I could apply to reach this Goal?

  • 1
    yes there is ... – ctac_ Apr 1 at 16:34

It's easier with perl:

perl -pi -e 's/\bL::\w++\K(?!\()/()/g' ./*.php
  • \bL::: L:: following a word boundary (to avoid replacing NIL::Foo ones).
  • \w: any word character (ASCII letters, numbers or underscore)
  • ++: one or more (of the \ws), but don't backtrack
  • \K: set the start of the matched portion (so just after that word)
  • (?!...): negative look ahead assertion. So here only matches if what follows is not a (.

Perl and the better regex engine is easier as it has lookaround feature, for GNU sed testing:

sed -E 's/(\w+::\w+)([^(].+)?/\1()\2/' ./*.php

solving recursive glob process:

find . -iname '*.php' -exec sed -E 's/(\w+::\w+)([^(].+)?/\1()\2/' '{}' +

for its real editing, add -i option,

find . -iname '*.php' -exec sed -i -E 's/(\w+::\w+)([^(].+)?/\1()\2/' '{}' +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.