Test-regex.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 Description
25  Tests for regular expressions
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IOstreams.H"
30 #include "IOobject.H"
31 #include "IFstream.H"
32 #include "regExp.H"
33 #include "List.H"
34 #include "Tuple2.H"
35 
36 using namespace Foam;
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 // Main program:
40 
41 int main(int argc, char *argv[])
42 {
43  List<Tuple2<string, string> > rawList(IFstream("testRegexps")());
44  Info<< "Test expressions:" << rawList << endl;
46 
47  List<string> groups;
48 
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);
55 
56  Info<< str << " =~ m/" << pat.c_str() << "/ == ";
57 
58  if (re.match(str, groups))
59  {
60  Info<< "true";
61  if (re.ngroups())
62  {
63  Info<< nl << "groups: " << groups;
64  }
65  }
66  else
67  {
68  if (re.search(str))
69  {
70  Info<< " partial match";
71  }
72  else
73  {
74  Info<< "false";
75  }
76  }
77  Info<< endl;
78  }
79 
80  Info<< nl << "test regExp(const char*) ..." << endl;
81  string me("Mark");
82 
83  // Handling of null strings
84  if (regExp(NULL).match(me))
85  {
86  Info<< "fail - matched: " << me << endl;
87  }
88  else
89  {
90  Info<< "pass - null pointer is no expression" << endl;
91  }
92 
93  // Normal match
94  if (regExp("[Mm]ar[ck]").match(me))
95  {
96  Info<< "pass - matched: " << me << endl;
97  }
98  else
99  {
100  Info<< "no match" << endl;
101  }
102 
103  // Match ignore case
104  if (regExp("mar[ck]", true).match(me))
105  {
106  Info<< "pass - matched: " << me << endl;
107  }
108  else
109  {
110  Info<< "no match" << endl;
111  }
112 
113  // Embedded prefix for match ignore case
114  if (regExp("(?i)mar[ck]").match(me))
115  {
116  Info<< "pass - matched: " << me << endl;
117  }
118  else
119  {
120  Info<< "no match" << endl;
121  }
122 
123  // Handling of empty expression
124  if (regExp("").match(me))
125  {
126  Info<< "fail - matched: " << me << endl;
127  }
128  else
129  {
130  Info<< "pass - no match on empty expression" << endl;
131  }
132 
133  // Embedded prefix - but expression is empty
134  if (regExp("(?i)").match(me))
135  {
136  Info<< "fail - matched: " << me << endl;
137  }
138  else
139  {
140  Info<< "pass - no match on empty expression" << endl;
141  }
142 
143 
144  Info<< endl;
145 
146  return 0;
147 }
148 
149 
150 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
List.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Tuple2.H
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::constant::atomic::me
const dimensionedScalar me
Electron mass.
Foam::regExp
Wrapper around POSIX extended regular expressions.
Definition: regExp.H:61
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
IFstream.H
IOobject.H
Foam::constant::atomic::re
const dimensionedScalar re
Classical electron radius: default SI units: [m].
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::IOobject::writeDivider
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:108
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
regExp.H
main
int main(int argc, char *argv[])
Definition: Test-regex.C:41