Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cannot find C++ regex tester

52 views
Skip to first unread message

RM

unread,
Sep 4, 2020, 10:45:15 AM9/4/20
to
I am programming PHP obfuscator in C++.
I want to encode PHP string literals. I want to encode PHP code treated
as text:

namespace app\assets;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}

into text:

namespace app\assets;
define('gkvxplzzsz_y_y_', 'yii\bootstrap\BootstrapAsset');
define('gbajlprgjb_y_y_', 'yii\web\YiiAsset'); define('ufapteqrhz_y_y_',
'css/site.css'); define('zgvjafbnir_y_y_', '@webroot');
define('gyoltyituv_y_y_', '@web');
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = zgvjafbnir_y_y_;
public $baseUrl = gyoltyituv_y_y_;
public $css = [
ufapteqrhz_y_y_,
];
public $js = [
];
public $depends = [
gbajlprgjb_y_y_,
gkvxplzzsz_y_y_,
];
}

To achieve this I wrote C++ function:

string insert_string_defines(const string &defines, const string &subject) {
size_t pos = subject.find("<?php");
if (pos == string::npos) {
return subject;
}
regex
re_namespace(R"((declare(\s|\n)*\([A-Za-z0-9_=\s]+\)(\s|\n)*;(\s|\n)*)*namespace\s+([A-Za-z0-9_\\]+)(\s|\n)+;)"
/* regex::extended */);
// optional namespace definition must occur before almost anything
(PHP syntax requirement):
string s = subject.substr(0, pos) +
regex_replace(subject.substr(pos), re_namespace, "$1\nnamespace $2;\n" +
defines);
if (s != subject) {
return s;
}
return subject.substr(0, pos) + "<?php\n" + defines + '\n' +
subject.substr(pos + 5);
}

I failed to find C++ regex tester in WWW, thus I don't know what's wrong
with my regex. Please help.

RM

unread,
Sep 5, 2020, 4:28:54 AM9/5/20
to
I tried to write my own C++ regex tester, but it doesn't work for
multiline input, I don't know why.


/*
Simple regex tester
Gets regex, reads text from standard input, writes matches to standard
output
*/

#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main(int argc, char *argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " regular_expression
<input_file" << endl;
return EXIT_FAILURE;
}
regex re(argv[1], std::regex_constants::multiline);
stringstream buffer;
buffer << cin.rdbuf();
string text = buffer.str();
cmatch cm;
regex_match(text.c_str(), cm, re);
for (unsigned int i = 0; i < cm.size(); ++i) {
cout << cm.str(i) << endl;
}
return EXIT_SUCCESS;
}

RM

unread,
Sep 5, 2020, 6:58:54 AM9/5/20
to
W dniu 05.09.2020 o 10:28, RM pisze:
I also tried (std::regex_constants::match_not_bol |
std::regex_constants::match_not_eol), without success.

match_not_bol Not Beginning-Of-Line The first character is not
considered a beginning of line ("^" does not match).
match_not_eol Not End-Of-Line The last character is not considered an
end of line ("$" does not match).

Paavo Helde

unread,
Sep 6, 2020, 4:09:38 AM9/6/20
to
This is more a regex question, not so much C++. I'm not a regex expert
either, but when I tested your regex with normal egrep command-line, it
seemed to work better when I used [\s\n] instead of (\s|\n).

Jorgen Grahn

unread,
Sep 6, 2020, 4:30:53 AM9/6/20
to
On Sun, 2020-09-06, Paavo Helde wrote:
> 04.09.2020 17:44 RM kirjutas:
...
>> I failed to find C++ regex tester in WWW, thus I don't know what's wrong
>> with my regex. Please help.
>
> This is more a regex question, not so much C++.

It could be a C++ question if there was something unique about
std::regex ... but the normal flavor of std::regex is ECMAScript, so
an online Javascript regex tester should be good enough. Options like
"multiline" most likely have direct equivalents, too.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

RM

unread,
Sep 12, 2020, 2:39:51 AM9/12/20
to
Finally I programmed a solution without regex.
Problem closed.

lew pitcher

unread,
Mar 12, 2021, 10:49:36 AM3/12/21
to
On Saturday, September 12, 2020 at 2:39:51 AM UTC-4, RM wrote:
> Finally I programmed a solution without regex.
> Problem closed.

I know that I'm late to this party, but ...
do you care to share your solution?
0 new messages