How to check if a string starts with ' " ' (double quotes)? [duplicate] - regex

This question already has answers here:
Regex for quoted string with escaping quotes
(17 answers)
Closed 8 years ago.
Every time I write " my compiler assumes I am trying to write a String. Instead I want my method to tell me if the incoming string starts with a double quote ""
Ex:
String n;
if(n==n.startsWith(" " " ));
doesn't work
Any suggestions??

You have to escape double quotes in string!
If you do it like this: " " ", string ends on second quotation mark. If in Java, you code should be like:
String n;
if(n.startsWith("\""))
{
// execute if true
}
Since you are matching just first character, you don't need to use such sophisticated tool as regular expressions:
String n;
if (n.charAt(0)=="\"")
{
// execute if true
}
BUT. You should make sure if string is not empty. Just for safety:
String n;
if (n.getText()!=null
&& !n.getText().isEmpty()
&& n.charAt(0)=="\"")
{
// execute if true
}
PS: space is a character.
PSS: flagged as dublicate.

Related

Replace text \n with actual new line. (C++) [duplicate]

This question already has answers here:
How to find and replace all occurrences of a substring in a string?
(9 answers)
Replace substring with another substring C++
(18 answers)
How to find and replace string?
(11 answers)
How do I replace all instances of a string with another string?
(6 answers)
Closed 6 months ago.
I'm using C++ and I have a problem. Instead of creating a new line it prints \n. My Code:
std::string text;
std::cout << text;
It prints:Hello\nWorld
It was supposed to read \n as a new line and print something like this:
"Hello
World"
So i've tried to use replace(text.begin(), text.end(), '\n', 'a') for testing purposes and nothing happened. It contiuned to print Hello\nWorld
std::replace() won't work in this situation. When called on a std::string, it can replace only single characters, but in your case your string actually contains 2 distinct characters '\' and 'n'. So you need to use std::string::find() and std::string::replace() instead, eg:
string::size_type index = 0;
while ((index = str.find("\\n", index)) != string::npos) {
str.replace(index, 2, "\n");
++index;
}

C++ RegEx for this pattern [duplicate]

This question already has answers here:
Regex statement in C++ isn't working as expected [duplicate]
(3 answers)
Closed 3 years ago.
I want to be able to find this pattern inside a c++ string. The pattern is as follows:
FIXED_WORD ANY_WORD(...)
where FIXED_WORD refers to a fixed keyword and ANY_WORD can be any word as long as a bracket follows from it.
I have tried using RegEx such as keyword \b(.*)\b\((.\*)\), where I tried to use the word boundary \b(.*)\b to extract out ANY_WORD followed by a bracket:
std::string s = "abcdefg KEYWORD hello(123456)";
std::smatch match;
std::regex pattern("KEYWORD \b(.*)\b\((.*)\)");
if (std::regex_search(s, match, pattern))
{
std::cout << "Match\n";
for (auto m : match)
std::cout << m << '\n';
}
else {
std::cout << "No match\n";
}
I am always getting a no match for this.
You're forgetting that slashes are escaped when you use a string literal. Use a raw string e.g. R"(...)" to preserve the slashes
std::regex pattern(R"(KEYWORD \b(.*)\b\((.*)\))");
Then your pattern works as expected:
Match
KEYWORD hello(123456)
hello
123456
https://godbolt.org/z/dJaAAX

How to replace all occurrences of a character with a given string [duplicate]

This question already has answers here:
How to replace all occurrences of a character in string?
(17 answers)
Replace part of a string with another string
(17 answers)
Closed 5 years ago.
I want to replace all occurrences of '$' with "$$". Currently I am using string::find to check if '$' is present in string or not. then I am using for loop and checking every character if it matches with '$'. If a character matches with $ then I am using string::replace to replace it.
Is there any other effective method to do this in c++? without traversing entire string with less complexity?
I don't know a standard function which replaces ALL occurrences of a certain string with another string. The replace-functions either replace all occurrences of a specific character by another character, or replace a single range of characters with another range.
So I think you cannot avoid iterating through the string on your own.
In your specific case, it might be easier, as you just have to insert an additional $ for every $-character you find. Note the special measure avoiding an endless loop, which would happen if one doubled also the $-value just inserted again and again:
int main() {
string s = "this $ should be doubled (i.e. $), then. But $$ has been doubled, too.";
auto it = s.begin();
while (it != s.end()) {
if (*it == '$') {
it = s.insert(it, '$');
it += 2;
}
else {
it++;
}
}
cout << s;
}

Regular expression to match input of n words separated by m spaces [duplicate]

This question already has answers here:
Regular expression capturing a repeated group
(1 answer)
c++ std::regex, smatch retains subexpressions only once for their apperance in a pattern string
(1 answer)
Closed 6 years ago.
So I'm learning regular expressions in c++11 and i'm trying to create a regular expression to match an input of N words separeted by M spaces.
So, for example, you input " word word word word ..." and you can continue like this for how long you like.
Now my problems come when I try to access the fields in the smatch variable after comparing an input to the regular expression. At the moment what I have is:
#include <regex>
regex input_reg(
"(?:[[:space:]]*"
"([[:alpha:]_]+)"
"[[:space:]]*)+");
smatch comparison;
if (regex_match(input, comparison, input_reg)){
for (smatch::size_type i = 0; i < comparison.size(); ++i){
cout << i << ": '" << comparison.str(i) << "'" << endl;
}
}
The problem with this is that for some reason, I get a match as I should but when I try to cout all the fields to see if it works I only get the initial match and the first field, nothing else:
0: ' word word word word '
1: 'word'
What am I doing wrong?
EDIT: The input is as seen in cout example of my code, it doesn't show all the spaces in the text for some reason.

Erase all occurences of a word from a string [duplicate]

This question already has answers here:
Replace part of a string with another string
(17 answers)
Closed 7 years ago.
I have a string such as:
AAAbbbbbAAA
I'd like to remove all the occurances of a pattern AAA to get:
bbbbb
The pattern can occur anywhere in the string.
Given:
string yourstring("AAAbbbAAA");
string removestring("AAA");
You could simply run something like this multiple times on your string:
yourstring.erase(yourstring.find(removestring), removestring.length());
Of course you will have to check that string::find actually finds an occurence before using string::erase.
Here is the code. It's not very much efficient, but works well, and is a tiny code.
string h = "AAAbbbAAAB";
int pos = h.find("AAA");
while(pos!=-1)
{
h.replace(pos, 3, "");
pos = h.find("AAA");
}
cout << h << endl;
It only works if you know the pattern. If it doesn't what you want, maybe you're looking for a pattern matching algorithm, like KMP.