Closed rdbo closed 1 year ago
This library seems to be the tool for the job: https://github.com/laurikari/tre It supports group capturing, has POSIX compliance and wide char support.
Example:
#include <tre/tre.h>
#include <locale.h>
#include <stdio.h>
int
main()
{
const char *text = "Hello, test";
const wchar_t *wtext = L"Привет, узер";
regex_t regex;
regmatch_t matches[2];
setlocale(LC_ALL, "");
tre_regcomp(®ex, "Hello, (.*)$", REG_EXTENDED);
tre_regexec(®ex, text, 2, matches, 0);
wprintf(L"%s\n", text);
wprintf(L"Username: %s\n", &text[matches[1].rm_so]);
tre_regfree(®ex);
tre_regwcomp(®ex, L"Привет, (.*)$", REG_EXTENDED);
tre_regwexec(®ex, wtext, 2, matches, 0);
wprintf(L"%ls\n", wtext);
wprintf(L"Username: %ls\n", &wtext[matches[1].rm_so]);
tre_regfree(®ex);
return 0;
}
Output:
Hello, test
Username: test
Привет, узер
Username: узер
Example CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(test-tre)
file(GLOB TRE_SRC "${PROJECT_SOURCE_DIR}/lib/*.c")
add_library(tre STATIC ${TRE_SRC})
target_compile_definitions(tre PUBLIC HAVE_ALLOCA=1 HAVE_ALLOCA_H=1 HAVE_MBRTOWC=1 HAVE_SYS_TYPES_H=0 HAVE_WCHAR_H=1 HAVE_MBSTATE_T=1 TRE_APPROX=1 TRE_MULTIBYTE=1 TRE_USE_ALLOCA=1 TRE_WCHAR=1 TRE_REGEX_T_FIELD=value)
add_executable(test test.c)
target_link_libraries(test PUBLIC tre)
target_include_directories(tre PUBLIC "${PROJECT_SOURCE_DIR}/include")
Had some problems adapting the already present regex functions to TRE's, the results seem to differ.
This may be a little too overkill for libmem. After all, how many times has someone needed to find a module through a regex, instead of just the module name? Closing for now.
Functions like
LM_GetModuleEx
andLM_GetProcessIdEx
could have regex support for more complex searching.