0

I wish to remove the classes from class properties in my PHP project. The string I want to find and replace look like this: private Selector $Selector; Keep in mind that the class (in this case "Selector") is variable.

I've already tried coming up with a solution myself, however, this pattern also matches private function sendEventRelayMessage( Tracker $

find private (.*?) \$\b

replace private \$

3
  • Instead of . use [^(] or [^(\n], or just replace .*? with \S+ if you only expect a single word between private and $... Commented Jan 14, 2020 at 14:37
  • What language/tool are you using? From the regex tag info: "Since regular expressions are not fully standardized, all questions with this tag should also include a tag specifying the applicable programming language or tool." Commented Jan 14, 2020 at 14:38
  • Ah sorry. Didn't think of that. I am trying to find and replace inside of IntelliJ So that's Regexp @Toto Commented Jan 14, 2020 at 14:41

1 Answer 1

1

You need a more specific Regex. If we follow PHP's rules on class name, it gives us a pretty good regex already to check class name. All you need is to remove reserved word and you got yourself a solution.

I've come up with this regex that does what you are looking for

private ((?!function)[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]+) .+

The regex will look for the keyword private, followed by a space and any characters that are valid class name caracter. It will filter out the keyword function (negative lookahead) then matches a spaces and anything after that.

Here is a more complete explaination on how the regex works.

Regular expression visualization

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.