简体   繁体   中英

Finding variable patterns using C++ STL Library

I have a file in which I have to look for a pattern.

The pattern is like this:

"ABCD"

A, B, C, D are variables which can be 0-9 and AF, and nothing else.

Few sample string in this file is :

at+creg=2 
OK 

at+creg? 
+CREG: 2,1,"03EB","3AC7" 
+CREG: 2,1,"03FC","9AC9" 

My pattern matching code should output:

03EB, 3AC7, 03FC, 9AC9

I want to do this using a functions from STL library.

If we have two strings, such as:

std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");  

then we can use

str.find(str2); 

to look for the pattern needle .

But in my case I do not have a fixed string, as A,B,C,D above are variable. So kindly help me to understand how to proceed here.

Regular expressions do what you want. Here is an example on how they work:

#include <regex>
#include <string>

using namespace std;

int main()
{
  // regular expressions which has the format you desire to match later
  regex isHexadecimal(R"(([[:d:]]|A|B|C|D|E|F)+)");
  string text = "asdlkfj 1C3AF";
  smatch result;

  regex_search (text, result, isHexadecimal);

  for (int i=0; i < result.size(); i++) {
      //result.position(i); // here are your positions
  }
  return 1;
}

All the magic happens in this line: regex isHexadecimal(R"(([[:d:]]|A|B|C|D|E|F)+)"); The R"(textstringtext)" syntax is for raw strings (strings in which you can have special characters without having to escape them). The regex itself is ([[:d:]]|A|B|C|D|E|F)+ : [[:d:]] is for digit. | means or. A , B are single characters to be matched. + means "one or more of".

Thus, the expression will catch a sequence with one or more digit(from 0 to 9) or A or B or C or D or E or F.

It seems you are searching hexadecimal digits. If the case of letters is not important then you can apply standard C function std::isxdigit declared in header <cctype> . Usually C standard functions are placed in the global namespace. So you may use ::isxdigit to distinguish it from the C++ function with the same name that has two parameters. Otherwise you can wrap the function in std::function or cast the function to required type.

Here is a demonstrative program. Instead of std::find_if algorithm I used std::copy_if algorithm just for the demonstrative purpose.

#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
#include <cctype>
#include <iterator>

int main()
{
    std::istringstream is( " ABC 123AA43 Jh12 76-12 fedcba1" );

    std::copy_if ( std::istream_iterator<std::string>( is ),
                   std::istream_iterator<std::string>(),
                   std::ostream_iterator<std::string>( std::cout, "\n" ),
                   []( const std::string &s )
                   {
                       return std::all_of( std::begin( s ), std::end( s ), ::isxdigit );
                   } );

}

The output is

ABC
123AA43
fedcba1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM