string contains valid characters - c++

I am writing a method whose signature is
bool isValidString(std::string value)
Inside this method I want to search all the characters in value are belongs to a set of characters which is a constant string
const std::string ValidCharacters("abcd")
To perform this search I take one character from value and search in ValidCharacters,if this check fails then it is invalid string is there any other alternative method in STL library to do this check.

Use find_first_not_of():
bool isValidString(const std::string& s) {
return std::string::npos == s.find_first_not_of("abcd");
}

you can use regular expressions to pattern match.
library regexp.h is to be included
http://www.digitalmars.com/rtl/regexp.html

Related

How can I check if the first char in a string is '-'?

In general, I need to check if a given string is a number. So I thought my function will check:
1. If the first char is '-' I want to check if there are only digits after it.
2. If the first char is 0 the length of the string has to be less than 3.
The problem: I cannot find a way to get the first char in the string, like if I would do it in C (just look if it is equal to ASCII number), nor in Java, where I would compare strings with equals().
Here's a handy utility function to parse numbers based on streams:
template <class T>
bool try_parse_number(std::string_view s, T& v, const std::locale& locale)
{
std::stringstream stream;
stream.imbue(locale);
stream << s;
stream >> v;
return !stream.fail();
}
Requires the includes <sstream>, <string_view> and <locale>, although you could strip the locale handling out.
You can further create a custom locale and a number facet to control number parsing to a greater degree.
I think in java it will be much easier since using a function --> s.charAt(0). you can easily take the first character of that string and can store that character and later you can compare that to anything.

Why can't I specify regex_constants in assign?

I am using std::regex with VS2015/VS2017 on Windows. I am trying to set up a general routine that will do a search search to perform the 8 functions as requested by the user:
Begins with
Does not begin with
Ends with
Does not end with
Contains string
Does not contain string
Contains any of the characters in supplied string
Does not contain any of the characters in supplied string.
The routine is passed the type above, the associated string, the string to search, a boolean variable to state if the search is case sensitive/insensitive and another boolean variable on whether to restrict the search for whole words only (types 1-6 only). I modify the supplied find string if "whole words only" is specified.
e.g.
void Search(std::wstring sRegex, std::wstring wsString, bool bCaseSensitive, bool bWholeWordsOnly);
However, I can't get basic case insensitivity to work as I expected.
This works:
std::wregex reg;
std::wsmatch m;
if (bCaseSensitive) {
reg.assign(sRegex.c_str(), std::regex::ECMAScript);
} else {
reg.assign(sRegex.c_str(), std::regex::ECMAScript | std::regex::icase);
}
std::regex_search(wsString, m, reg) .....
but this doesn't (icase ignored and worse):
std::wregex reg;
std::wsmatch m;
reg.assign(sRegex.c_str(), std::regex::ECMAScript |
(bCaseSensitive ? 0 : std::regex::icase));
std::regex_search(wsString, m, reg) .....
Can anyone explain this please?
PS. I haven't yet coded more than type 1 yet as I want to fix this first before continuing. Any suggestions for the regex string for the other types with & without "whole word only" (where appropriate) would be gratefully received.

How to detect ESC in string using Boost regex

I need to determine if a file is PCL encoded. So I am looking at the first line to see if it begins with an ESC character. If you know a better way feel free to suggest. Here is my code:
bool pclFlag = false;
if (containStr(jobLine, "^\\e")) {
pclFlag=true;
}
bool containStr(const string& s, const string& re)
{
static const boost::regex e(re);
return regex_match(s, e);
}
pclFlag does not get set to true.
You've declared boost::regex e to be static, which means it will only get initialized the very first time your function is called. If your search here is not the first call, it will be searching for whatever string was passed in the first call.
regex_match must match the entire string. Try adding ".*" (dot star) to the end of your regex.
Important
Note that the result is true only if the expression matches the whole of the input sequence. If you want to search for an expression somewhere within the sequence then use regex_search. If you want to match a prefix of the character string then use regex_search with the flag match_continuous set.
http://www.boost.org/doc/libs/1_51_0/libs/regex/doc/html/boost_regex/ref/regex_match.html
#JoachimPileborg is right... if (jobline[0] == 0x1B) {} is much easier.
Boost.Regex seems like overkill if all you want to do is see if a string starts with a certain character.
bool pclFlag = jobLine.length() > 0 && jobLine[0] == '\033';
You could also use Boost string algorithms:
#include <boost/algorithm/string.hpp>
bool pclFlag = jobLine.starts_with("\033");
If you're looking to see if a string contains an escape anywhere in the string:
bool pclFlag = jobLine.find('\033') != npos;

Regular Expression for removing suffix

What is the regular expression for removing the suffix of file names? For example, if I have a file name in a string such as "vnb.txt", what is the regular expression to remove ".txt"?
Thanks.
Do you really need a regular expression to do this? Why not just look for the last period in the string, and trim the string up to that point? Frankly, there's a lot of overhead for a regular expression, and I don't think you need it in this case.
As suggested by tstenner, you can try one of the following, depending on what kinds of strings you're using:
std::strrchr
std::string::find_last_of
First example:
char* str = "Directory/file.txt";
size_t index;
char* pStr = strrchr(str,'.');
if(nullptr != pStr)
{
index = pStr - str;
}
Second example:
int index = string("Directory/file.txt").find_last_of('.');
If you are using Qt already, you could use QFileInfo, and use the baseName() function to get just the name (if one exists), or the suffix() function to get the extension (if one exists).
If you're looking for a solution that will give you anything except for the suffix, you should use string::find_last_of.
Your code could look like this:
const std::string removesuffix(const std::string& s) {
size_t suffixbegin = s.find_last_of('.');
//This will handle cases like "directory.foo/bar"
size_t dir = s.find_last_of('/');
if(dir != std::string::npos && dir > suffixbegin) return s;
if(suffixbegin == std::string::npos) return s;
else return s.substr(0,suffixbegin);
}
If you're looking for a regular expression, use \.[^.]+$.
You have to escape the first ., otherwise it will match any character, and put a $ at the end, so it will only match at the end of a string.
Different operating systems may allow different characters in filenams, the simplest regex might be (.+)\.txt$. Get the first capture group to get the filename sans extension.

How to use string and string pointers in C++

I am very confused about when to use string (char) and when to use string pointers (char pointers) in C++. Here are two questions I'm having.
which one of the following two is correct?
string subString;
subString = anotherString.sub(9);
string *subString;
subString = &anotherString.sub(9);
which one of the following two is correct?
char doubleQuote = aString[9];
if (doubleQuote == "\"") {...}
char *doubleQuote = &aString[9];
if (doubleQuote == "\"") {...}
None of them are correct.
The member function sub does not exist for string, unless you are using another string class that is not std::string.
The second one of the first question subString = &anotherString.sub(9); is not safe, as you're storing the address of a temporary. It is also wrong as anotherString is a pointer to a string object. To call the sub member function, you need to write anotherString->sub(9). And again, member function sub does not exist.
The first one of the second question is more correct than the second one; all you need to do is replace "\"" with '\"'.
The second one of the second question is wrong, as:
doubleQuote does not refer to the 10th character, but the string from the 10th character onwards
doubleQuote == "\"" may be type-wise correct, but it doesn't compare equality of the two strings; it checks if they are pointing to the same thing. If you want to check the equality of the two strings, use strcmp.
In C++, you can (and should) always use std::string (while remembering that string literals actually are zero-terminated character arrays). Use char* only when you need to interface with C code.
C-style strings need error-prone manual memory management, need to explicitly copy strings (copying pointers doesn't copy the string), and you need to pay attention to details like allocating enough memory to have the terminating '\0' fit in, while std::string takes care of all this automagically.
For the first question, the first sample, assuming sub will return a substring of the provided string.
For the second, none:
char doubleQuote = aString[9];
if( doubleQuote == '\"') { ... }
Erm, are you using string from STL?
(i.e. you have something like
#include <string>
#using namespace std;
in the beginning of your source file ;) )
then it would be like
string mystring("whatever:\"\""");
char anElem = mystring[9];
if (anElem=="\"") { do_something();}
or you can write
mystring.at(9)
instead of square brackets.
May be these examples can help.