I have a simple regex to check if a function name, lies within a block of C-code (the actual name finding code is written in Java).
For this example, the function name i’m trying to find is called putFillerData. The code block looks like this:
static void fillerSwapByteOrder(int t_database, tag_t t_message, char* data_buff, int* pos)
{
short data_length = trxDBGetNodeSize(t_database);
if (!data_length) return;
char *data = umalloc(data_length);
if (data_length == sizeof(short))
{
short s_data = 0;
shareGReadData(t_database, &s_data);
short nbo_data = htons(s_data);
memcpy(data, &nbo_data, sizeof(short));
}
else if (data_length == sizeof(int))
{
int s_data = 0;
shareGReadData(t_database, &s_data);
int nbo_data = htonl(s_data);
memcpy(data, &nbo_data, sizeof(int));
}
else
{
ufree(data);
return;
}
putFillerData(t_message, data, data_length, data_buff, pos);
ufree(data);
}
The regex statement i’m using looks like this:
Pattern.matches("\\b" + Pattern.quote(name) + "\\b", code);
However, this always return false… Why?
The reason I’m using this regex, is becouse .contains finds substrings as well, which is not what I want…
.matches()only returns True if the entire string matches the regex.You want to use the
.find()method:Also,
\bonly matches between alphanumeric and non-alphanumeric characters. So it should be fine if yournameisputFillerData, but not if it’s something likeputFillerData().