| 1 | // |
|---|---|
| 2 | // grep.cpp |
| 3 | // |
| 4 | // This sample demonstrates the RegularExpression class. |
| 5 | // |
| 6 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 7 | // and Contributors. |
| 8 | // |
| 9 | // SPDX-License-Identifier: BSL-1.0 |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #include "Poco/RegularExpression.h" |
| 14 | #include <iostream> |
| 15 | |
| 16 | |
| 17 | using Poco::RegularExpression; |
| 18 | |
| 19 | |
| 20 | int main(int argc, char** argv) |
| 21 | { |
| 22 | if (argc < 2) |
| 23 | { |
| 24 | std::cout << "usage: "<< argv[0] << ": [-i] [-x] pattern"<< std::endl; |
| 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | std::string pattern; |
| 29 | int options = 0; |
| 30 | for (int i = 1; i < argc; ++i) |
| 31 | { |
| 32 | std::string arg(argv[i]); |
| 33 | if (arg == "-i") |
| 34 | options += RegularExpression::RE_CASELESS; |
| 35 | else if (arg == "-x") |
| 36 | options += RegularExpression::RE_EXTENDED; |
| 37 | else |
| 38 | pattern = arg; |
| 39 | } |
| 40 | |
| 41 | RegularExpression re(pattern, options); |
| 42 | |
| 43 | int c = std::cin.get(); |
| 44 | while (c != -1) |
| 45 | { |
| 46 | std::string line; |
| 47 | while (c != -1 && c != '\n') |
| 48 | { |
| 49 | line += (char) c; |
| 50 | c = std::cin.get(); |
| 51 | } |
| 52 | |
| 53 | RegularExpression::Match mtch; |
| 54 | if (re.match(line, mtch)) |
| 55 | std::cout << line << std::endl; |
| 56 | |
| 57 | if (c != -1) c = std::cin.get(); |
| 58 | } |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 |