开发者

RegExp string filtering - how to allow `-` sign?

So having such code that filters letters, and / ".:= how to allow -?

#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>

std::string filter_args(std::string args)
{
    std::cout << boost::erase_all_regex_copy开发者_如何学Python(args, boost::regex("[^a-zA-Z0-9=\"/.: ]+")); 
    return boost::erase_all_regex_copy(args, boost::regex("[^a-zA-Z0-9=\"/.: ]+"));
}


Boost's default Regex behaviour is Perl-compatible, and man perlre says:

You can specify a character class, by enclosing a list of characters in [], which will match any character from the list. If the first character after the "[" is "^", the class matches any character not in the list. Within a list, the "-" character specifies a range, so that a-z represents all characters between "a" and "z", inclusive. If you want either "-" or "]" itself to be a member of a class, put it at the start of the list (possibly after a "^"), or escape it with a backslash. "-" is also taken literally when it is at the end of the list, just before the closing "]". (The following all specify the same class of three characters: [-az], [az-], and [a\-z]. All are different from [a-z], which specifies a class containing twenty-six characters, even on EBCDIC-based character sets.) Also, if you try to use the character classes \w, \W, \s, \S, \d, or \D as endpoints of a range, the "-" is understood literally.


So:

boost::erase_all_regex_copy(args, boost::regex("[^a-zA-Z0-9=\"/.: -]+"))

or

boost::erase_all_regex_copy(args, boost::regex("[^a-zA-Z0-9=\\-\"/.: ]+"))

(notice the double-backslash; one to escape for the string literal, and the second to escape for the regex).

I recommend the former.


Always check out the documentation as your first port of call!


Escape it with \. That should work. \-.

You will actually have to do it twice. \\- One time to escape the \, the second to escape the -.


Put it last inside the brackets, like [^a-zA-Z0-9=\"/.: -].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜