I have a line which have data coming with "=" symbols. i need to ignore all white spaces before and after "=" symbol in my string
example:
input i have: "this is test = test1 and test1= test2"
output I am looking for:
"this is test=test1 and test1=test2"
I have tried with istream ignore function and std::find function for string but not sure how can i remove trailing spaces unless a non-whites pace character occurs in the string.
I found a similar question here but it is not answered.
:
https://stackoverflow.com/questions/24265598/delimiter-is-getting-added-at-the-beginning-of-each-line-of-a-delimited-file-whi
Thanks
Ruchi
If the other whitespace may be replaced by a single space, then you can read in all words from the string (std::cin <<), write them in a new string separated by a space and handle the needed tokens like "=" in this case by putting it in the string without spaces. You will probably need some "spaceNeeded" flags to handle no space before and after the token.
Related
This question already has answers here:
How can I get double quotes into a string literal?
(2 answers)
Closed 2 years ago.
I am having issues detecting double quotes ("") or quotation marks in general from a string.
I tried using the str.find(""") or str.find("""") however, the first one doesn't compile and the second does not find the location. It returns 0. I have to read a string from a file, for example:
testFile.txt
This is the test "string" written in the file.
I would read the string using and search it
string str;
size_t x;
getline(inFile, str);
x = str.find("""");
however the value returned is 0. Is there another way to find the quotation marks that enclose 'string'?
The string """" doesn't contain any quotes. It is actually an empty string: when two string literals are next to each other, they get merged into just one string literal. For example "hello " "world" is equivalent to "hello world".
To embed quotes into a string you can do one of the following:
Escape the quotes you want to have inside your string, e.g. "\"\"".
Use a raw character string, e.g. R"("")".
You should use backslash to protect your quotes.
string a = str.find("\"")
will find ".
The " character is a special one, used to mark the beginning and end of a string literal.
String literals have the unusual property that consecutive ones are concatenated in translation phase 6, so for example the sequence "Hello" "World" is identical to "HelloWorld". This also means that """" is identical to "" "" is identical to "" - it's just a long way to write the empty string.
The documentation on string literals does say they can contain both unescaped and escaped characters. An escape is a special character that suppresses the special meaning of the next character. For example
\"
means "really just a double quote, not with the special meaning that it begins or ends a string literal".
So, you can write
"\""
for a string consisting of a single double quote.
You can also use a character literal since you only want one character anyway, with the 4th overload of std::string::find.
my use case is the follow:
String text_string: "text1:message1,text3:message3,text2:message,..."
select regexp_replace(text_string, '[^:]*:([^,]*(,|$))', '$1')
Correct output: message1,message3,message2,...
The pattern work, but the problem is that if there is a character ":" o "," in the message the replace doesn't work.
So I tried to use "::" and ",," characters as a separators in the string
String text_string: "text1::message1,,text3::message3,,text2::message2,..."
select regexp_replace(text_string, '[^::]*::([^,,]*(,,|$))', '$1')
Correct output: message1,,message3,,message2,,...
but also in this case, if there is one ":" or "," character in the string (in the text or in the message) the replace command doesn't work.
How should the regular expression be modified to work?
Delimiters cannot be characters that are likely to be in the data. Since you have control over it, use pipes '|' or tildes '~' maybe. Only you can come up with the right characters by analyzing the data.
If you can't do that, then you'll need to put quotes around the data that contains the delimiter character and come up with a way to deal with that.
In these two cases I am entering \n as user input (in a string) in one and I am making \n as a part of string in the program itself (no user input):
string str1;
cin>>str1; //case 1 - \n entered as the part of the input
string str="hello\n"; //case 2
in case 1 \n is considered as a part of the input string whereas in case 2 it is considered as newline - why?
Escape sequences are compiler-time only literals. When your compiler comes across a \ in a string, it looks for a pattern afterwards to determine the value.
When you read in from the console, the input is read one character at a time.
Most debuggers will show the inputted string as "hello\\n", or broken up into individual characters:
'h','e','l','l','o','\\','n'
When you manually set the string in the code, such as string str = "hello\n", the compiler recognizes the escape sequence and treats it as the single character '\n'. This allows programmers to have shorthand for printing characters without going and printing their ASCII values.
Users, on the other hand, have an enter button to conveniently add a newline, and programs are generally oriented to have a human-readable interface(i.e., if I type a '\' character I expect it to be a '\' if I have no experience with computers)
Another note about cin is that it uses newline characters other whitespace to distinguish between kinds of input. The function getline is meant for string input for getting around this, but the stream extraction is done from whitespace to whitespace so it is consistent with all data types(int,float,char,etc)
I have a long list of strings formatted as follows:
"astringsmaller alongstringthatisgreaterthan32characterstomatch"
"alongstringthatisgreaterthan32characterstomatch astringsmaller"
"astring string alongstringthatisgreaterthan32 598931"
Using regex, how would I match a string where that string is greater than 31 characters using the space a delimiter?
For example, the string below would be "seen" as two parts and the counts would be 14 and 47 thus be a valid match.
"astringsmaller alongstringthatisgreaterthan32characterstomatch"
Unfortunately, the number of delimiters/spaces are not consistent in position or amount. I also have a bunch of other special characters that would be considered as "delimiters"
("!")
("#")
('"')
("#")
("$")
("&")
("'")
("(")
(")")
("*")
("+")
(",")
(".")
("/")
(":")
(";")
("<")
("=")
(">")
("?")
("^")
("`")
("{")
("|")
("}")
("~")
(" ")
(" ")
("“")
("”")
("’")
("%")
Thanks in advance!
In that case, what you want to do is match 31+ characters which are not delimiters:
[^!#"#$&'()*+,./:;<=>?^`{|}~ “”’%]{31,}
Demo
Also, instead of using a delimiter "blacklist", you maybe could also maybe only match valid words? (but that depends on your exact use case)
\w{31,}
(\w is the same as [a-zA-Z0-9_])
Demo
As far as I understand this you want to validate lines if they contain a string that is greater than 31 characters (not containing separator). I would suggest using a lookahead like this:
^(?=.*[^!#"#$&'))*+,.\/:;<=>?^`{|}~ “”’%\r\n]{31,}).+$
Demo
Then, you can further process those lines as needed.
I have a string lots\t of\nwhitespace\r\n which I have simplified but I still need to get rid of the other spaces in the string.
QString str = " lots\t of\nwhitespace\r\n ";
str = str.simplified();
I can do this erase_all(str, " "); in boost but I want to remain in qt.
str = str.simplified();
str.replace( " ", "" );
The first changes all of your whitespace characters to a single instance of ASCII 32, the second removes that.
Try this:
str.replace(" ","");
Option 1:
Simplify the white space, then remove it
Per the docs
[QString::simplified] Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space.
Once the string is simplified, the white spaces can easily be removed.
str.simplified().remove(' ')
Option 2:
Use a QRegExp to capture all types of white space in remove.
QRegExp space("\\s");
str.remove(space);
Notes
The OPs string has white space of different types (tab, carriage return, new line), all of which need to be removed. This is the tricky part.
QString::remove was introduced in Qt 5.6; prior to 5.6 removal can be achieved using QString::replace and replacing the white space with an empty string "".
You can omit the call to simplified() with a regex:
str.replace(QRegularExpression("\\s+"), QString());
I don't have measured which method is faster. I guess this regex would perform worse.