1

I have the following function, written in C++11, that uses regular expressions from the standard regex module. As its name suggests, it determines whether the given string str is a number prefixed with the given prefix.

bool isPrefixedNumber(const std::string &prefix, const std::string &str) { std::regex re(prefix + "[[:digit:]]+")) return std::regex_match(str, re); } 

The problem is that I want prefix to be taken literally, i.e. I want

isPrefixedNumber("t.st", "tXst123") 

to return false. Is there a way of constructing such a regular expression without the need to manually escape all occurrences of special characters in prefix? In another words, how to prevent the interpretation of special characters when passing a std::string into std::regex?

Note: The above function is just a simple illustration of my question. I need to prevent interpretation of strings in a much larger regular expression. I do not need another way of re-writing the function without regular expressions.

2 Answers 2

1

str.find(prefix) == 0 and str.find_first_not_of("0123456789", prefix.size()) != std::string::npos are the two things you want.

Whenever you have matching requirements, consider that a regex is actually a program that is parsed and executed at runtime. Put this in contrast with writing a few lines of C++ code and then decide which is better, but don't aim for regexes from the start.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes-- seems like this might be an X/Y problem.
I totally agree with you that this is a better approach for this particular example. However, I need to prevent interpretation of strings in a much larger regular expression. I just came up with this simple example to illustrate my question. I will update the question to include this fact.
Okay. Still, when you say "don't interpret", it means verbatim use and therefore simple string matching, for which you should at least consider simple searching. That said, check out stackoverflow.com/questions/1252992/…, it seems to answer your question.
I was kind of hoping there would be a built-in way of doing this so I don't have to do it manually. Anyway, thanks!
File a feature request for Boost, this is indeed something that is a reasonably common need and one that can easily be done wrong (e.g. by forgetting one of the many special characters).
0

I'd probably just match the two pieces separately, something along this general line:

std::string::size_type pos; std::regex re(prefix + "[[:digit:]]+")); return ((pos = str.find(prefix)) != std::string::npos) && std::regex_match(str.substr(pos+prefix.size()), re)); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.