-1

I'm upgrading to Laravel 5.7 and as part of that I need to update all or in Blade to ??, wherever they are surrounded by curly brackets ({{ }} or {!! !!}). I'm trying to use Regex in PhpStorm to find all instances of this (i.e. {*or*} where * could be any number of any characters).

How would I do this?

I did try this myself but I am new to Regex and my attempt is probably pathetically bad:

/{[^!][or]}/
user6122500
  • 892
  • 1
  • 15
  • 31
  • Try `(?:\G(?!\A)|{)[^{}]*?\K\bor\b(?=[^{}]*})` – Wiktor Stribiżew Feb 03 '19 at 20:45
  • Why do you have `or` inside square brackets? That matches an `o` or `r`, it doesn't match the string `or`. This seems to be a perennial mistake that regex newbies make, not sure why. – Barmar Feb 03 '19 at 21:02
  • @WiktorStribiżew thanks for the suggestion. Unfortunately doesn't seem to work in PHPStorm - searching for that yields no results – user6122500 Feb 03 '19 at 22:08
  • @WiktorStribiżew I'd change the expression a little bit to ```(?:(?!^)|{)[^{}]*?\K(or)(?=[^{}]*})```; now it matches ```{{}or}``` and any *or* after a closing brackets. – Leonardo Maffei Feb 04 '19 at 03:31
  • @user6122500 You might have problems with search settings, see [my regex is working](https://regex101.com/r/HDrdAI/2) and matches any number of `or` occurrences in between curly braces. – Wiktor Stribiżew Feb 04 '19 at 08:43

2 Answers2

2

I wouldn't use a regular expression to try to accomplish this, mostly because of reasons outlined in this article, which dives a little deeper into computer theory. Basically regular expressions can't "remember" contextual opening tags such as {{ or {!! to pair them to an end tag; a regular expression that matches what you're trying to do (finding {{ something here }} or {!! something here, too !!} would also match this {{ something wrong !!}. That said, you could write your own converter, which would be able to maintain that context, or go through the heavy lifting of manually doing it. You could also do some research to find out if anyone else has already done it. A regular expression, however, is likely the wrong tool for the job.

Michael Miller
  • 399
  • 1
  • 9
0

I've found that the following is close to a solution for this use-case (can be tweaked a bit as needed):

\{(.*)\bor\b(.*)\}

e.g. to just match double {{, you can use:

\{\{(.*)\bor\b(.*)\}\}

(in PHPStorm you may need to click the filter checkbox that says "Except Comments")

Doesn't work perfectly, so if anyone has a better solution please post!

user6122500
  • 892
  • 1
  • 15
  • 31