Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / applications / test / regex / Test-regex.C
blobb4fe0a8755334c71a7e54a6c5712f173dca9e16e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 Description
26 \*---------------------------------------------------------------------------*/
28 #include "IOstreams.H"
29 #include "IOobject.H"
30 #include "IFstream.H"
31 #include "regExp.H"
32 #include "List.H"
33 #include "Tuple2.H"
35 using namespace Foam;
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 // Main program:
40 int main(int argc, char *argv[])
43     List<Tuple2<string, string> > rawList(IFstream("testRegexps")());
44     Info<< "input list:" << rawList << endl;
45     IOobject::writeDivider(Info) << endl;
47     List<string> groups;
49     // report matches:
50     forAll(rawList, elemI)
51     {
52         const string& pat = rawList[elemI].first();
53         const string& str = rawList[elemI].second();
54         regExp re(pat);
56         Info<< str << " =~ m/" << pat.c_str() << "/ == ";
58         if (re.match(str, groups))
59         {
60             Info<< "true";
61             if (re.ngroups())
62             {
63                 Info<< " groups:" << groups;
64             }
65         }
66         else
67         {
68             Info<< "false";
69             if (re.search(str))
70             {
71                 Info<< " partial match";
72             }
73         }
74         Info<< endl;
75     }
77     Info<<"test regExp(const char*) ..." << endl;
78     string me("Mark");
80     if (regExp("[Mm]ar[ck]").match(me))
81     {
82         Info<< "matched: " << me << endl;
83     }
84     else
85     {
86         Info<< "no match" << endl;
87     }
89     if (regExp("").match(me))
90     {
91         Info<< "matched: " << me << endl;
92     }
93     else
94     {
95         Info<< "no match" << endl;
96     }
98     if (regExp(NULL).match(me))
99     {
100         Info<< "matched: " << me << endl;
101     }
102     else
103     {
104         Info<< "no match" << endl;
105     }
107     Info<< endl;
109     return 0;
113 // ************************************************************************* //