SuN
SuN

Reputation: 1056

Find all occurrences of a specific character in a string between parentheses, which is after another string itself (REGEX)

I'm trying to replace / with a . using Regex (and Atom Editor) so that $app->call( 'text/'. $var .'/e ); becomes $app->call( 'text.'. $var .'.e );

But with no luck so far, here is what I came up with:

(?<=\()[^)]?[\/]+(?=\))

For some reason it only catches the forward slash when it's the only character of the string: https://regex101.com/r/zgPl5F/1

What am I missing here?

My idea is to target a specific class to bulk edit parameters passed to its functions (in Atom Editor) so that $app->xxx(*/*) becomes $app->xxx(*.*) (where xxx is always a function name). So altogether:

^(\$app->\w)+(?<=\()[^)]?[\/]+(?=\))

Edit: based on @Wiktor's answer I tried this, but it does not work:

^(\$app->\w)+(\/(?=[^()]*\)))+

NB. It's not exactly the same question.

Upvotes: 1

Views: 273

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626960

You may use the following solution in Notepad++ or SublimeText:

(?:\G(?!\A)|^\$app->\w+\()[^()]*?\K/(?=[^()]*\))

See the regex demo.

Details

  • (?:\G(?!\A)|^\$app->\w+\() - end of a previous match (\G(?!\A)) or (|) start of a line, $app->, 1+ word chars and ( (see ^\$app->\w+\()
  • [^()]*? - 0 or more, but as few as possible, chars other than ( and )
  • \K - match reset operator
  • / - a slash
  • (?=[^()]*\)) - followed with 0+ chars other than ( and ) and then a ).

Upvotes: 1

Related Questions