Distinguish between strings
Mar 22, 2015 at 7:58pm UTC
How can I distinguish between the following strings?
and
The code I am trying to improve is:
1 2 3 4 5 6 7 8 9 10 11
std::regex rx ("\\|" );
std::regex info("[\\+>\\^~]" );
strnltrim = strnl;
std::remove(strnltrim.begin(), strnltrim.end(), ' ' );
std::remove(strnltrim.begin(), strnltrim.end(), '|' );
if (std::regex_search(strnl, rx, std::regex_constants::match_continuous) && (std::regex_search(strnlprev, string, std::regex_constants::match_continuous)) && strnlprevempty.empty() && !std::regex_match(strnltrim, info))//empty separating-line
{
}
else if (std::regex_search(strnl, rx, std::regex_constants::match_continuous) )//infoline
{
}
Maybe checking if the separating-line does not contain the regex info? Or checking if the separating-line only contains '|' and ' '? I do not get it working, please help.
Mar 23, 2015 at 4:43am UTC
2 thoughts
If the 2nd line is considered empty(not counting the pipes), and the entire line is the string, then you can just see if each line is equal to a empty line.
1 2 3 4 5 6 7
string blankline="| || ||" ;
string strn1="| || ||" ;
if (strn1==blankline)
{cout << "True" ;}
else
{cout << "False" ;}
If that's not possible, then just check each char between the pipes seems like it would work.
Mar 23, 2015 at 5:09am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <iostream>
#include <string>
#include <regex>
int main()
{
const std::string bar = "\\|" ; // escaped bar
const std::string space = "\\s+" ; // one or more spaces (escaped white space)
const std::string cap = "\\^" ;
const std::string plus = "\\+" ;
const std::regex pattern_one( bar + space + bar + bar + space + bar + bar ) ;
const std::regex pattern_two( bar + cap + space + plus + space + bar + bar + space + plus + space + bar + bar ) ;
const std::string string_one = "| || ||" ;
const std::string string_two = "|^ + || + ||" ;
if ( std::regex_match( string_one, pattern_one ) ) std::cout << "string_one matched pattern_one\n" ;
if ( std::regex_match( string_one, pattern_two ) ) std::cout << "string_one matched pattern_two\n" ;
if ( std::regex_match( string_two, pattern_one ) ) std::cout << "string_two matched pattern_one\n" ;
if ( std::regex_match( string_two, pattern_two ) ) std::cout << "string_two matched pattern_two\n" ;
}
http://coliru.stacked-crooked.com/a/b4e46dee83f85830
Topic archived. No new replies allowed.