Hello! I want to share a solution to a single problem related to auto-replace by regular expressions in
NetBeans .
Description of the problem
When implementing many projects, the programmer has to solve the
localization problem, and in particular the implementation of multilingual user interface support. I have often come across this task and used to do this for a list (array) of tokens that were called by key in the right place (hereinafter, we will discuss
PHP projects, but the article will help any developers who use NetBeans IDE):
<?php
$_CONFIG['errors'] = array();
$_CONFIG['errors']['db_connection'] = "Database connection error:";
$_CONFIG['errors']['no_user'] = "No user with this e-mail and password.";
$_CONFIG['errors']['wrong_user_data'] = "Wrong user data.";
...
?>
But this solution has several disadvantages:
- in order to see exactly which value is inserted into the code, you must constantly switch to the tab with the language file and look for the desired value;
- inconveniences arising from multiple embedding of arrays (in the example, level 2 nesting, but maybe more);
- This data structure is a variable, and for such a task it would be more logical to use constants, since the value of these tokens does not change during the execution of the script.
Formulation of the problem
To solve the problem described above, we
decompose it into subtasks, according to the problems described:
- to implement the ability to quickly transition to the value of a token from any part of the code in the development environment;
- select the main types of tokens and build the most convenient and understandable structure of their representation (in large projects the number of tokens can reach several thousand)
- use constants instead of variables;
Thus, it is most convenient to solve this problem with the help of classes of constants:
<?php
class __ERRORS {
/**
* Database errors
*/
const DB_CONNECTION = "Database connection error:";
const NO_USER = "No user with this e-mail and password.";
const WRONG_USER_DATA = "Wrong user data.";
/**
* Forms validation errors
*/
const WRONG_CAPTCHA = "Wrong captcha code";
.....
?>
This approach will allow to break lexemes into necessary classes. You can simply create classes
__TIPS ,
__MESSAGES ,
__WARNINGS , etc. Also, when you click on a class constant in any part of the code in the IDE with
Ctrl pressed, we immediately go to its declaration, which greatly speeds up the development process.
')
Problem i encountered
Still, one problem remains, what to do if we already have a huge file with lexemes in arrays. Many systems are written with this approach to localization. There are a lot of lexemes, it is natural to interrupt them in a new structure often very difficult and long. The logical solution seems to be autochange in the IDE itself.
Here we come to what I am writing an article for. First of all, in order to change the storage structure of the tokens, I used NetBeans AutoCorrect using the regular expression
\ $ \ _ CONFIG \ ['errors' \] \ ['(. +)' \] .

Next, I needed to rename all constants to upper case, and then I was at a dead end. Started looking for how to do this in NetBeans. And even on
official sources . It turned out that auto-change can not be done. Then I remembered the
macros . It remains only to write another regular expression to search in the code of constant names and write a macro that will translate the found constant names to uppercase.
To do this, go to
Edit -> Start Macro Recording and look for a regular expression
([az] {1} [a-z _] *) =
Stop recording the macro
Edit -> Stop Macro Recording and give it a name.
Go to the
Macros control panel
Tools -> Options -> Editor -> Macros and find our macro.

We add the command to transfer to uppercase
to-upper-case and assign a key combination to quickly call a macro. Now we have the ability to quickly
refactor our code.

Conclusion
This article discusses the solution of a specific practical problem. But it was chosen simply as an example, rather, even historically, it was the impetus for finding such a solution. But the steps described above can help with many of these tasks related to the need to quickly find and replace a large amount of code in NetBeans IDE. I hope that this article will help someone.
Literature
PS The author of the article just needs an invite.
UPD Author
dzarezenko received an invite. Thank you so much!