Arabic regex matching - c++

Viewed 185

I need to find the given string has arabic letters. It ranges from \u0600-\u06FF\u0750-\u077F.

I have written the below program:

std::vector<STD_STRING> strFieldvalues;
std::string pattern = "/[\u0600-\u06FF\u0750-\u077F]/";
std:string strFieldVal;
gboolArabic = false;
int i = 0;
int j = 0;
for ( ;i < fieldValues.size() && j< fieldNames.size(); i++,j++) //for loop its entering
{
    strFieldVal=fieldValues[i].GetPString();
    if (std::regex_match(strFieldVal, std::regex("(sub)(/[\u0600-\u06FF\u0750-\u077F]/)")))
    {
        gboolArabic = true;
        gArabicFieldNames.push_back(fieldNames[j].GetPString());
    }
}

strFieldVal is coming as <0067><062A><0627>. But its not entering into the if block. Can anyone help .

Sample program given below is working in online compiler. In visual studio, not entering into the if block. Adding screenshots. enter image description here enter image description here

2 Answers

It appears you need to remove regex delimiters on both ends of the regex and apply a + quantifier to the regex pattern because regex_match requires a full string match:

#include <iostream>
#include <regex>

int main() {
    std::string strFieldVal("المتحدة");
    std::regex pattern("[\u0600-\u06FF\u0750-\u077F]+");
    if (std::regex_match(strFieldVal, pattern))
    {
        std::cout << strFieldVal << " is Arabic.\n";
    }
    return 0;
}

See the C++ demo, output: المتحدة is Arabic..

#include <iostream>
#include <regex>


int main() {
    std::wstring strFieldVal(L"المتحدة");
    std::wregex pattern(L"[\u0600-\u06FF\u0750-\u077F]+");
    if (std::regex_match(strFieldVal, pattern))
    {
        std::cout << strFieldVal << " is Arabic.\n";
    }
    return 0;
}

The above one works correctly.In Visual Studio When i Add c++ source file and add this content, its asking for encoding,i given Yes. Then it worked perfectly.

Related