Regex

Dec 4, 2018 at 6:41pm
Write your question here.
Need help with regex statement to recognise: 1. Binary numbers only. 2. Hexa decimal numbers only
Dec 4, 2018 at 7:23pm
closed account (SECMoG1T)
hello there:

"(0|1)+" - recognisez binary numbers only, where 0 0r 1 must occur atleast once e.g
0,1,111,100101011 etc.

"0(x|X)[0-9a-fA-F]+" - recognizes hexadecimal numbers only, but the must begin with the
sequence 0X or 0x , but you can chenge that if it isn't a requirement for you

here is some test code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <regex>

int main()
{
    std::string hexa("0(x|X)[0-9a-fA-F]+"), binary("(0|1)+"),data{};

    std::regex hexa_obj(hexa),bin_obj(binary);

    std::cout<<"testing binary numbers\n\n";

    for(int i=0;i <10;i++)
    {
        std::cout<<">> ";
        std::getline(std::cin,data,'\n');

        if(std::regex_match(data.begin(),data.end(),bin_obj))
            std::cout<<data<<"  is binary\n";
        else
            std::cout<<data<<"  isn't binary\n";
    }

    std::cout<<"\n\testing hexadecimal numbers\n\n";

    for(int i=0;i <10;i++)
    {
        std::cout<<">> ";
        std::getline(std::cin,data,'\n');

        if(std::regex_match(data.begin(),data.end(),hexa_obj))
            std::cout<<data<<"  is hexa\n";
        else
            std::cout<<data<<"  isn't hexa\n";
    }
}


NB: the above expressions follow the modified ECMAscript syntax, default for c++ regex.
Last edited on Dec 8, 2018 at 9:22pm
Dec 8, 2018 at 8:40pm
i am receiving this message: ||=== Build: Debug in reall (compiler: GNU GCC Compiler) ===|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|
C:\Users\dell\Documents\reall\main.cpp||In function 'int main()':|
C:\Users\dell\Documents\reall\main.cpp|7|warning: extended initializer lists only available with -std=c++11 or -std=gnu++11|
C:\Users\dell\Documents\reall\main.cpp|7|error: in C++98 'data' must be initialized by constructor, not by '{...}'|
C:\Users\dell\Documents\reall\main.cpp|9|error: 'regex' is not a member of 'std'|
C:\Users\dell\Documents\reall\main.cpp|18|error: 'regex_match' is not a member of 'std'|
C:\Users\dell\Documents\reall\main.cpp|18|error: 'bin_obj' was not declared in this scope|
C:\Users\dell\Documents\reall\main.cpp|31|error: 'regex_match' is not a member of 'std'|
C:\Users\dell\Documents\reall\main.cpp|31|error: 'hexa_obj' was not declared in this scope|
||=== Build failed: 7 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
Dec 8, 2018 at 9:09pm
closed account (SECMoG1T)
you must have at least a c++11 compiler to use the std::regex library, please find a suitable compiler and retest the code.
Dec 8, 2018 at 10:22pm
This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|

Do what it told you. Use the option -std=c++11 or -std=gnu++11.
Dec 8, 2018 at 10:35pm
closed account (SECMoG1T)
error This file requires compiler and library support for the ISO C++ 2011 standard.

i'd advice you to get an up to date compiler, able to support all the new language features, unless you are forced to use c98 compiler probably for code compatibility.
Last edited on Dec 8, 2018 at 10:40pm
Dec 9, 2018 at 6:43am
Thanks very much Yolanda and Repeater... Worked as expected. Bravo Yolanda for the code.
Dec 10, 2018 at 5:00am
Hello....I am a new member in this forum site...I have no idea in this binary number program what is the output of this program anyone please let me know once
[url=https://www.certvalue.com/iso-22000-certification-dubai/]ISO 22000 Certification in Dubai[/url] | [url=https://www.certvalue.com/iso-9001-certification-in-turkey/]ISO 9001 Audit in Turkey [/url]
Topic archived. No new replies allowed.