E
E
Evgeny Proff2017-09-19 23:33:08
C++ / C#
Evgeny Proff, 2017-09-19 23:33:08

How can I convert a string variable to a regular expression?

#include <algorithm>
#include <iostream>
#include <math.h>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <map>
#include <tuple>
#include <set>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <cstdio>
#include <regex>



using namespace std;



int main() {
  string word, mask;
  cin >> word >> mask;

  for (size_t i = 0; i < mask.size(); ++i) {

    if (mask[i] == '?') {	
      mask[i] = '.';
    }

    if (mask[i] == '*') {	
      mask[i] = '.';
      mask.insert(i + 1, "*");
    }
  }

  mask += "$";

  regex reg = mask;                                                                 //Вот здесь ошибка

  regex_match(word, reg) ? cout << "YES" : cout << "NO";





  system("pause");
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2017-09-20
@EvgenyProff

maybe like this:
regex reg(mask);

E
Evgeny Proff, 2017-09-20
@EvgenyProff

string word, mask;
  cin >> word >> mask;

  for (size_t i = 0; i < mask.size(); ++i) {

    if (mask[i] == '?') {	
      mask[i] = '.';
    }

    if (mask[i] == '*') {	
      mask[i] = '.';
      mask.insert(i + 1, "*");
    }
  }

  mask += "$";
  char *regular = new char[mask.size()];

  for (int i = 0; i < mask.size(); ++i) {
    regular[i] = mask[i];
  }

  regex reg = (regular);

  regex_match(word, reg) ? cout << "YES" : cout << "NO";

it also doesn't work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question