Thread View: pl.comp.programming
5 messages
5 total messages
Started by RM
Mon, 06 Apr 2020 16:09
problem z wyrażeniem regularnym w C++
Author: RM
Date: Mon, 06 Apr 2020 16:09
Date: Mon, 06 Apr 2020 16:09
5 lines
1143 bytes
1143 bytes
Chciałbym w C++ dla line == "private $ci;" wyodrębnić wyrażeniem
regularnym identyfikator "ci". Napisałem w C++:
regex_match(line, matches2, regex("\\$((\\w|\\d|_)+)")))
i to wywołanie zwraca mi false. Co robię źle?
Używam #include <regex> (w Linuxie). W PHP to wyrażanie regularne mi działa.
Re: problem z wyrażeniem regularnym w C++
Author: RM
Date: Tue, 07 Apr 2020 17:07
Date: Tue, 07 Apr 2020 17:07
66 lines
3137 bytes
3137 bytes
Potrzebuję jeszcze pomocy przy przetumaczeniu kawałka kodu z PHP na C++,
bo dokumentacji do C++owego sub_match nie rozumiem. Problem jest dla
mnie za trudny.
W PHP mam:
preg_match_all($r, $line, $matches, PREG_OFFSET_CAPTURE))
...
$match = $matches[$indexes[$what][$k]];
$delta_str = 0;
foreach ($match as $m) {
if ($what == 'f' && $k == 1) { // 2nd regexp for functions
// check if a method call:
$before = substr($line, $m[1] - 2, 2);
if ($before == '->' || $before == '::') continue; // don't obfuscate now
}
if ($what == 'f') { // case insensitive
$n = array_search(strtoupper($m[0]), $from[$what]);
} elseif ($what == 'm') { // case insensitive
if (in_array(strtoupper($m[0]), $from['a'])) { // action
$n = FALSE;
} else {
$n = array_search(strtoupper($m[0]), $from[$what]);
}
} else {
$n = array_search($m[0], $from[$what]);
}
if ($n !== FALSE) {
$line = substr_replace($line, '_' . $to[$what][$n], $m[1] +
$delta_str, strlen($m[0]));
$delta_str += strlen($to[$what][$n]) - strlen($m[0]) + 1;
}
}
W C++ wyobrażam sobie tak:
short delta_str = 0;
sub_match match = matches[indexes[what][k]];
for (auto m = match.begin(); m < match.end(); m++) {
if (what == i_functions && k == 1) { // 2nd regexp for functions
// check if a method call:
string before = line.substr(m.position() - 2, 2);
if (before == "->" || before == "::") {
continue; // don't obfuscate now
}
}
if (what == i_functions) { // case insensitive
n = strvector_search(strtoupper(m.str()), from[what]);
} else if (what == i_methods) { // case insensitive
if (in_strvector(strtoupper(m.str()), from[i_actions])) { // action
n = -1;
} else {
n = strvector_search(strtoupper(m.str()), from[what]);
}
} else { // case sensitive
n = strvector_search(m.str(), from[what]);
}
if (n >= 0) {
line = substr_replace(line, string("_").append(to[what][n]),
m.position() + delta_str, strlen(m.str().c_str()));
delta_str += strlen(to[what][n].c_str()) - strlen(m.str().c_str()) + 1;
}
}
Ale sub_match nie zawiera metod begin() i end() więc mój kod się nie
kompiluje. Prosiłbym bardzo o poprawienie kodu.
Re: problem z wyrażeniem regularnym w C++
Author: RM
Date: Wed, 08 Apr 2020 13:49
Date: Wed, 08 Apr 2020 13:49
12 lines
1702 bytes
1702 bytes
Mój program to obsucator języka PHP i potrzebuję wyodrębnić wszysztkie identyfikatory ([\\w|\\d|_)+ (właściwości OOP) dla wyrażenia regularnego "(static)?\\s*(private|protected|public)\\s+(static)?\\s*(\\$((\\w|\\d|_)+),?\\s*)+\\s*[^\\(]". Tzn. problem polega na tym jak w C++ wyodrębnić wszystkie ciągi pasujące do grupy (...) numer indexes[what][k] wyrażenia regularnego. Z tego co się zorientowałem (ale nie jestem tego pewien) w przeciwieństwie do PHP C++ wrzuca wszystkie dopasowania do jednej tablicy matches albo znajduje tylko jedno dopasowanie sub_match dla każdej grupy (...). Ja natomiast potrzebuję znaleźć wszystkie ciągi pasujące do grupy ((\\w|\\d|_)+) (numer indexes[what][k]) a nie tylko jeden.
Re: problem z wyrażeniem regularnym w C++
Author: RM
Date: Wed, 08 Apr 2020 14:39
Date: Wed, 08 Apr 2020 14:39
2 lines
1030 bytes
1030 bytes
Czy w C++ Regex są nazwane grupy przechwytujące i można ich użyć z sub_match?
Re: problem z wyrażeniem regularnym w C++
Author: Wojciech Muła
Date: Thu, 09 Apr 2020 19:44
Date: Thu, 09 Apr 2020 19:44
16 lines
2155 bytes
2155 bytes
On Monday, April 6, 2020 at 6:09:09 PM UTC+2, RM wrote:
> Chciałbym w C++ dla line == "private $ci;" wyodrębnić wyrażeniem
> regularnym identyfikator "ci". Napisałem w C++:
> regex_match(line, matches2, regex("\\$((\\w|\\d|_)+)")))
> i to wywołanie zwraca mi false. Co robię źle?
> Używam #include <regex> (w Linuxie). W PHP to wyrażanie regularne mi działa.
regex_match: Determines if the regular expression e matches the entire target character sequence.
regex_search: Determines if there is a match between the regular expression e and some subsequence in the target character sequence.
Użyj tej drugiej funkcji.
Poza tym chyba masz błąd w wyrażeniu, złapie też $1234.
w.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads