Find sequence of chars in string c++ and erase it [duplicate] - c++

This question already has answers here:
Replace part of a string with another string
(17 answers)
Closed 6 years ago.
I need find all occurrences of sequence: \r\n(some hex number)\r\n and delete this sequences from my string. Hexadecimal number doesn't start with 0x or x. It's just 20bb for example.
These sequences are chunks in http 1.1 protocol. I can't find them with string.find, maybe some regex would help.
Thanks for help.

From the code here I made this:
std::string string("\r\n20BB\r\n");
string = std::regex_replace(string,
std::regex("\r\n[0-9A-Fa-f]+\r\n"), "");
It should work. The [0-9A-Fa-f]+ captures one or more hex digits.

Related

How do i "subtract" a character from a string in c++ [duplicate]

This question already has answers here:
Remove last character from C++ string
(12 answers)
Closed 8 months ago.
Lets say I have a string variable with the value "bananas" in it. I want to subtract the last letter so the string becomes "banana". I am quite a newbie, so I dont even know how to tackle this.
Just use the pop_back() function.
Try this code, it 'subtracts' the last character:
std::string str = "bananas";
str.pop_back();

Regex how prevent string containing only the "?" character [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 2 years ago.
I'm searching for a Regex to avoid letting user to enter a string with only a single ? character.
Here are examples of what I want to achieve:
? Not Ok (I want to avoid letting this)
?a Ok
a? Ok
?? Ok
??? Ok
This matches either any one- character string which is different from ? or any string with at least two characters
^([^?]|..+)$

What's the regular expression for pipe-delimited number? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I want to check that a String is pipe-generated numbers. There should be numbers between pipes.
Valid Strings examples: 300, 300|600.
Invalid Strings examples: 300||||600
I tried ^([\d|\d])*$. However, this still said that 300||||600 is a valid String.
Here, what you want is one number, ie \d+, followed by an undetermined number of occurrences of a pipe then a number, which would be (\|\d+)* (the pipe is escaped).
As you want it to cover the whole input this would be
^\d+(\|\d+)*$

regex to grab int/floats and exclude text [duplicate]

This question already has answers here:
RegEx for both, integer and float [closed]
(3 answers)
Closed 4 years ago.
trying to write the appropriate regex expression to capture barometric pressure with two string possibilities. looking to simply grab the float values and remove the "in" string.
The String possibilities are (examples):
'30.01in'
or
'30in'
my current expression (see below) works for the former (30.01), but fails to grab the float in the latter (30in)
re.compile('[0-9]?[0-9]\...')
(\d+(?:\.\d+)?)in
This should capture ints or floats

regex string length validaiton [duplicate]

This question already has answers here:
How to find the length of a string in R
(6 answers)
Closed 8 years ago.
In R, I need to remove string that exceeds the length of 7 characters, from a column in a data frame.
My code is,
memos.to <- as.data.frame(apply(memos.to,2,function(x)gsub('/^[a-zA-Z0-9]{7,}$/', NA ,x)))
and it doesn't seem to work. What's wrong here?
The easiest way is to just check the string length.
Don't know R lang, but all things being equal, if it conforms to the minimal modern regex's
One of these should match as far as regex is concerned
/.{8,}/ using Dot-all modifier as external flag
or
/(?s).{8,}/
or
/[\S\s]{8,}/ if Dot-all not available
if you are only considering [a-zA-Z0-9] chars
/^[a-zA-Z0-9]{8,}$/