From 0a9c36b286ba28cde494a1d559263eb9c33c5d2e Mon Sep 17 00:00:00 2001 From: "Stephen M. McQuay" Date: Fri, 25 May 2012 08:49:38 -0600 Subject: [PATCH] Added some regex examples. --- Makefile | 6 ++++-- regex1.cc | 28 ++++++++++++++++++++++++++++ regex2.cc | 28 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 regex1.cc create mode 100644 regex2.cc diff --git a/Makefile b/Makefile index 2ae4280..ede5934 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ CPPFLAGS=-Wall -g -std=c++0x -pthread # CXX=clang++ # CPPFLAGS=-Wall -g -std=c++0x -stdlib=libc++ -L/opt/clang/lib -pthread -all: auto constexpr ilists init init_new lambda loops thread thread2 tuple +all: auto constexpr ilists init init_new lambda loops regex1 regex2 thread thread2 tuple run: all ./auto @@ -25,7 +25,9 @@ run: all ./thread ./thread2 ./tuple + echo "1 11 1.1 1.1E12 stephen" | ./regex2 + echo "hello cruel 12 world 12.22. you are 12.12 awesome!!" | ./regex2 clean: - @rm -vf auto constexpr ilists init init_new lambda loops thread thread2 tuple + @rm -vf auto constexpr core* ilists init init_new lambda loops regex1 regex2 thread thread2 tuple diff --git a/regex1.cc b/regex1.cc new file mode 100644 index 0000000..00e6a51 --- /dev/null +++ b/regex1.cc @@ -0,0 +1,28 @@ +// from https://github.com/sol-prog/regex_tutorial +#include +#include +#include + +using namespace std; + +int main() { + string input; + + // traditional pcre escape + regex int_re {"(\\+|-)?\\d+"}; + + // ecmascript + regex float_re {"((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?((e|E)((\\+|-)?)[[:digit:]]+)?"}; + + while(cin >> input){ + if (regex_match(input, int_re)) { + cout << "integer" << endl; + } + else if(regex_match(input, float_re)) { + cout << "float" << endl; + } + else { + cout << "nan" < +#include +#include + +using namespace std; + +int main() +{ + //This should match any real number + regex number {"((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?((e|E)((\\+|-)?)[[:digit:]]+)?\\b"}; + //This should match any word + regex word {"[[:alpha:]]+"}; + regex space {"\\s+"}; + string input, clean_words, clean_numbers; + //Replace with an empty string + const string format {""}; + + getline(cin,input); + + //Split the input string in numbers and words + clean_numbers=regex_replace(input, number, format); + clean_words=regex_replace(input, word, format); + + cout << clean_words << endl; + cout << clean_numbers << endl; + return 0; +} +