Reputation: 1573
I am trying to get use a regex pattern to detect if a dll file-name:
module_
.dll
Here is the code I'm using:
bool IsModule(std::string const& name)
{
static std::regex const regex("^module_[:alnum:]+\\.dll");
return std::regex_match(name, regex);
}
Using an online regex debugger, I couldn't figure the issue out. When I test it using module_custom.dll
as the input file-name, it does not see it as a match.
Upvotes: 1
Views: 683
Reputation: 43743
The [:alnum:]
character class does not appear to be supported by C++ 11 regex engine. According to this page, it is equivalent to [a-zA-Z0-9]
, so you could just do this:
bool IsModule(std::string const& name)
{
static std::regex const regex("^module_[a-zA-Z0-9]+\\.dll$");
return std::regex_match(name, regex);
}
Note that I added the $
anchor at the end, as Mike mentioned in the comments above. If you omit that, it will consider something like module_custom.dllfoo
to be a valid match.
The [:alnum:]
character class is a peculiarity of the POSIX regex flavor. I'm not familiar with regex in C++, but it appears that the default engine is compatible with EMACScript rather than POSIX. According to this page, however, it looks like it may be possible to switch it to use a POSIX compatable engine? If using POSIX regex syntax is important to you, perhaps someone more knowledgeable could jump in with more information about that.
Upvotes: 4
Reputation: 1478
try this one
bool IsModule(std::string const& name)
{
static std::regex const regex("^module_\\w+\\.dll");
return std::regex_match(name, regex);
}
Not sure if :alphanum: works with C++ 11.
Upvotes: 1