RegEx - Match all unquoted strings in brackets except if they are all capital-letters

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



RegEx - Match all unquoted strings in brackets except if they are all capital-letters



In PHP 7.2, the manual says:



Unquoted strings that are non-existent global constants are taken to
be strings of themselves. This behaviour used to emit an E_NOTICE, but
will now emit an E_WARNING. In the next major version of PHP, an Error
exception will be thrown instead.



Also, code like this $list[products_name], will now produce this warning:


$list[products_name]



Warning: Use of undefined constant products_name - assumed 'products_name'
(this will throw an Error in a future version of PHP) in %s on line %d



I have now switched to PHP 7.2 and got such code that I would like to search and replace, by matching all unqoted strings inside brackets and add single-quotes to them (eg. $list[products_name] will become $list['products_name']), except for strings that are all capital. The reason I want to skip those specifically is because many times they are indeed constants and I shouldn't put them inside quotes.
I am new to regex and tried several options. So far, I have come up with this regex:


$list[products_name]


$list['products_name']


$w+[([a-zA-Z_]+[w]+)]



It works well for most purposes I need it, except it still captures all capital strings in code blocks like this:


$options[$list[_option_id]]=array($list[_value],$list[_value_id]);
$option_names[$list[_option_id1]]=$list[_option];
$product_name=$list[products_name];
$product_name=$list[0];
$product_name=$list[DONTCAPTUREME];
$product_name=$list[CapTureMe];
$product_name=$list[CapTurEME];



DEMO



How can I modify it to not match only examples like DONTCAPTUREME?


DONTCAPTUREME



Thank you




2 Answers
2



Right after the first [, use negative lookahead for all-caps followed by ]:


[


]


$w+[(?![A-Z]+])([a-zA-Z_]+[w]+)]
^^^^^^^^^^^^



https://regex101.com/r/I91TNn/3





I know it is not part of the original question, but how can I make it also skip DONT_CAPTURE_ME. I have tried with $w+[(?![A-Z_]+])([a-zA-Z_]+[w]+)] but that also skips many other ones that I do need to capture. So I basically need to skip all caps and all caps+_ only.
– Nikita 웃
Aug 5 at 23:28


DONT_CAPTURE_ME


$w+[(?![A-Z_]+])([a-zA-Z_]+[w]+)]





Just add _ to the character set in the negative lookahead regex101.com/r/I91TNn/4 Another option is positive lookahead for anything but a ], repeated, followed by a lowercase $w+[(?=[^]]*[a-z])([a-zA-Z_]+[w]+)]
– CertainPerformance
Aug 5 at 23:30



_


]


$w+[(?=[^]]*[a-z])([a-zA-Z_]+[w]+)]





also skips many other ones Can you replicate the problem on regex101? I'm curious
– CertainPerformance
Aug 5 at 23:34



also skips many other ones





I can't, works fine on regex101. I am using grepWin, which usually works great with these, but for some reason - not this time. It does pick lines like $actionKey = $get[AM_ACTION_GET_VARIABLE]; Any other way to do this regex?
– Nikita 웃
Aug 5 at 23:37



$actionKey = $get[AM_ACTION_GET_VARIABLE];





The issue was that the program was set to search case insensitive by default. Changing this to case sensitive regex search fix that problem. Thanks again.
– Nikita 웃
Aug 6 at 17:14



Finally the solution that worked best for me (using grepWin) is:



Search (case sensitive must be enabled):


(").*?1(*SKIP)(*FAIL)|$w+]?[(?!w*(|w*:)(?=[^]]*[a-z])(K[a-zA-Z_]+[w]+)



Replace with:


'2'



Always backup your files before such operation.



DEMO



Screenshot Here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard