-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathregular-expressions.html
More file actions
158 lines (127 loc) · 62.4 KB
/
regular-expressions.html
File metadata and controls
158 lines (127 loc) · 62.4 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>C++ | Regular expressions</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Basic regex_match and regex_search Examples, regex_iterator Example, Anchors, regex_replace Example, regex_token_iterator Example, Splitting a string, Quantifiers">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="C++ | Regular expressions">
<meta property="og:description" content="Basic regex_match and regex_search Examples, regex_iterator Example, Anchors, regex_replace Example, regex_token_iterator Example, Splitting a string, Quantifiers">
<meta property="og:type" content="article">
<meta property="og:url" content="/cpp/regular-expressions.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="C++ | Regular expressions">
<meta name="twitter:description" content="Basic regex_match and regex_search Examples, regex_iterator Example, Anchors, regex_replace Example, regex_token_iterator Example, Splitting a string, Quantifiers">
<meta name="twitter:url" content="/cpp/regular-expressions.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/702.1a82db2e.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>C++</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/cpp/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/cpp/getting-started-with-cpp.html" class="sidebar-link">Getting started with C++</a></li><li><a href="/cpp/literals.html" class="sidebar-link">Literals</a></li><li><a href="/cpp/operator-precedence.html" class="sidebar-link">operator precedence</a></li><li><a href="/cpp/floating-point-arithmetic.html" class="sidebar-link">Floating Point Arithmetic</a></li><li><a href="/cpp/bit-operators.html" class="sidebar-link">Bit Operators</a></li><li><a href="/cpp/bit-manipulation.html" class="sidebar-link">Bit Manipulation</a></li><li><a href="/cpp/bit-fields.html" class="sidebar-link">Bit fields</a></li><li><a href="/cpp/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/cpp/iterators.html" class="sidebar-link">Iterators</a></li><li><a href="/cpp/basic-input-output-in-c.html" class="sidebar-link">Basic input/output in c++</a></li><li><a href="/cpp/loops.html" class="sidebar-link">Loops</a></li><li><a href="/cpp/file-i-o.html" class="sidebar-link">File I/O</a></li><li><a href="/cpp/cpp-streams.html" class="sidebar-link">C++ Streams</a></li><li><a href="/cpp/stream-manipulators.html" class="sidebar-link">Stream manipulators</a></li><li><a href="/cpp/flow-control.html" class="sidebar-link">Flow Control</a></li><li><a href="/cpp/metaprogramming.html" class="sidebar-link">Metaprogramming</a></li><li><a href="/cpp/const-keyword.html" class="sidebar-link">const keyword</a></li><li><a href="/cpp/mutable-keyword.html" class="sidebar-link">mutable keyword</a></li><li><a href="/cpp/friend-keyword.html" class="sidebar-link">Friend keyword</a></li><li><a href="/cpp/type-keywords.html" class="sidebar-link">Type Keywords</a></li><li><a href="/cpp/basic-type-keywords.html" class="sidebar-link">Basic Type Keywords</a></li><li><a href="/cpp/variable-declaration-keywords.html" class="sidebar-link">Variable Declaration Keywords</a></li><li><a href="/cpp/keywords.html" class="sidebar-link">Keywords</a></li><li><a href="/cpp/returning-several-values-from-a-function.html" class="sidebar-link">Returning several values from a function</a></li><li><a href="/cpp/polymorphism.html" class="sidebar-link">Polymorphism</a></li><li><a href="/cpp/references.html" class="sidebar-link">References</a></li><li><a href="/cpp/value-and-reference-semantics.html" class="sidebar-link">Value and Reference Semantics</a></li><li><a href="/cpp/c-function-call-by-value-vs-call-by-reference.html" class="sidebar-link">C++ function "call by value" vs. "call by reference"</a></li><li><a href="/cpp/copying-vs-assignment.html" class="sidebar-link">Copying vs Assignment</a></li><li><a href="/cpp/pointers.html" class="sidebar-link">Pointers</a></li><li><a href="/cpp/pointers-to-members.html" class="sidebar-link">Pointers to members</a></li><li><a href="/cpp/the-this-pointer.html" class="sidebar-link">The This Pointer</a></li><li><a href="/cpp/smart-pointers.html" class="sidebar-link">Smart Pointers</a></li><li><a href="/cpp/classes-structures.html" class="sidebar-link">Classes/Structures</a></li><li><a href="/cpp/function-overloading.html" class="sidebar-link">Function Overloading</a></li><li><a href="/cpp/operator-overloading.html" class="sidebar-link">Operator Overloading</a></li><li><a href="/cpp/function-template-overloading.html" class="sidebar-link">Function Template Overloading</a></li><li><a href="/cpp/virtual-member-functions.html" class="sidebar-link">Virtual Member Functions</a></li><li><a href="/cpp/inline-functions.html" class="sidebar-link">Inline functions</a></li><li><a href="/cpp/special-member-functions.html" class="sidebar-link">Special Member Functions</a></li><li><a href="/cpp/non-static-member-functions.html" class="sidebar-link">Non-Static Member Functions</a></li><li><a href="/cpp/constant-class-member-functions.html" class="sidebar-link">Constant class member functions</a></li><li><a href="/cpp/c-containers.html" class="sidebar-link">C++ Containers</a></li><li><a href="/cpp/namespaces.html" class="sidebar-link">Namespaces</a></li><li><a href="/cpp/header-files.html" class="sidebar-link">Header Files</a></li><li><a href="/cpp/using-declaration.html" class="sidebar-link">Using declaration</a></li><li><a href="/cpp/std-string.html" class="sidebar-link">std::string</a></li><li><a href="/cpp/std-array.html" class="sidebar-link">std::array</a></li><li><a href="/cpp/std-vector.html" class="sidebar-link">std::vector</a></li><li><a href="/cpp/std-map.html" class="sidebar-link">std::map</a></li><li><a href="/cpp/std-optional.html" class="sidebar-link">std::optional</a></li><li><a href="/cpp/std-function-to-wrap-any-element-that-is-callable.html" class="sidebar-link">std::function: To wrap any element that is callable</a></li><li><a href="/cpp/std-forward-list.html" class="sidebar-link">std::forward_list</a></li><li><a href="/cpp/std-pair.html" class="sidebar-link">std::pair</a></li><li><a href="/cpp/std-atomics.html" class="sidebar-link">std::atomics</a></li><li><a href="/cpp/std-variant.html" class="sidebar-link">std::variant</a></li><li><a href="/cpp/std-iomanip.html" class="sidebar-link">std::iomanip</a></li><li><a href="/cpp/std-any.html" class="sidebar-link">std::any</a></li><li><a href="/cpp/std-set-and-std-multiset.html" class="sidebar-link">std::set and std::multiset</a></li><li><a href="/cpp/std-integer-sequence.html" class="sidebar-link">std::integer_sequence</a></li><li><a href="/cpp/using-std-unordered-map.html" class="sidebar-link">Using std::unordered_map</a></li><li><a href="/cpp/standard-library-algorithms.html" class="sidebar-link">Standard Library Algorithms</a></li><li><a href="/cpp/the-iso-c-standard.html" class="sidebar-link">The ISO C++ Standard</a></li><li><a href="/cpp/inline-variables.html" class="sidebar-link">Inline variables</a></li><li><a href="/cpp/random-number-generation.html" class="sidebar-link">Random number generation</a></li><li><a href="/cpp/date-and-time-using-chrono-header.html" class="sidebar-link">Date and time using header</a></li><li><a href="/cpp/sorting.html" class="sidebar-link">Sorting</a></li><li><a href="/cpp/enumeration.html" class="sidebar-link">Enumeration</a></li><li><a href="/cpp/iteration.html" class="sidebar-link">Iteration</a></li><li><a href="/cpp/regular-expressions.html" aria-current="page" class="active sidebar-link">Regular expressions</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/cpp/regular-expressions.html#basic-regex-match-and-regex-search-examples" class="sidebar-link">Basic regexmatch and regexsearch Examples</a></li><li class="sidebar-sub-header"><a href="/cpp/regular-expressions.html#regex-iterator-example" class="sidebar-link">regex_iterator Example</a></li><li class="sidebar-sub-header"><a href="/cpp/regular-expressions.html#anchors" class="sidebar-link">Anchors</a></li><li class="sidebar-sub-header"><a href="/cpp/regular-expressions.html#regex-replace-example" class="sidebar-link">regex_replace Example</a></li><li class="sidebar-sub-header"><a href="/cpp/regular-expressions.html#regex-token-iterator-example" class="sidebar-link">regextokeniterator Example</a></li><li class="sidebar-sub-header"><a href="/cpp/regular-expressions.html#splitting-a-string" class="sidebar-link">Splitting a string</a></li><li class="sidebar-sub-header"><a href="/cpp/regular-expressions.html#quantifiers" class="sidebar-link">Quantifiers</a></li></ul></li><li><a href="/cpp/implementation-defined-behavior.html" class="sidebar-link">Implementation-defined behavior</a></li><li><a href="/cpp/exceptions.html" class="sidebar-link">Exceptions</a></li><li><a href="/cpp/lambdas.html" class="sidebar-link">Lambdas</a></li><li><a href="/cpp/value-categories.html" class="sidebar-link">Value Categories</a></li><li><a href="/cpp/preprocessor.html" class="sidebar-link">Preprocessor</a></li><li><a href="/cpp/data-structures-in-c.html" class="sidebar-link">Data Structures in C++</a></li><li><a href="/cpp/templates.html" class="sidebar-link">Templates</a></li><li><a href="/cpp/expression-templates.html" class="sidebar-link">Expression templates</a></li><li><a href="/cpp/curiously-recurring-template-pattern-crtp.html" class="sidebar-link">Curiously Recurring Template Pattern (CRTP)</a></li><li><a href="/cpp/threading.html" class="sidebar-link">Threading</a></li><li><a href="/cpp/thread-synchronization-structures.html" class="sidebar-link">Thread synchronization structures</a></li><li><a href="/cpp/the-rule-of-three-five-and-zero.html" class="sidebar-link">The Rule of Three, Five, And Zero</a></li><li><a href="/cpp/raii-resource-acquisition-is-initialization.html" class="sidebar-link">RAII: Resource Acquisition Is Initialization</a></li><li><a href="/cpp/rtti-run-time-type-information.html" class="sidebar-link">RTTI: Run-Time Type Information</a></li><li><a href="/cpp/mutexes.html" class="sidebar-link">Mutexes</a></li><li><a href="/cpp/recursive-mutex.html" class="sidebar-link">Recursive Mutex</a></li><li><a href="/cpp/semaphore.html" class="sidebar-link">Semaphore</a></li><li><a href="/cpp/futures-and-promises.html" class="sidebar-link">Futures and Promises</a></li><li><a href="/cpp/atomic-types.html" class="sidebar-link">Atomic Types</a></li><li><a href="/cpp/type-erasure.html" class="sidebar-link">Type Erasure</a></li><li><a href="/cpp/explicit-type-conversions.html" class="sidebar-link">Explicit type conversions</a></li><li><a href="/cpp/unnamed-types.html" class="sidebar-link">Unnamed types</a></li><li><a href="/cpp/type-traits.html" class="sidebar-link">Type Traits</a></li><li><a href="/cpp/return-type-covariance.html" class="sidebar-link">Return Type Covariance</a></li><li><a href="/cpp/layout-of-object-types.html" class="sidebar-link">Layout of object types</a></li><li><a href="/cpp/type-inference.html" class="sidebar-link">Type Inference</a></li><li><a href="/cpp/typedef-and-type-aliases.html" class="sidebar-link">Typedef and type aliases</a></li><li><a href="/cpp/type-deduction.html" class="sidebar-link">type deduction</a></li><li><a href="/cpp/trailing-return-type.html" class="sidebar-link">Trailing return type</a></li><li><a href="/cpp/alignment.html" class="sidebar-link">Alignment</a></li><li><a href="/cpp/perfect-forwarding.html" class="sidebar-link">Perfect Forwarding</a></li><li><a href="/cpp/decltype.html" class="sidebar-link">decltype</a></li><li><a href="/cpp/sfinae-substitution-failure-is-not-an-error.html" class="sidebar-link">SFINAE (Substitution Failure Is Not An Error)</a></li><li><a href="/cpp/undefined-behavior.html" class="sidebar-link">Undefined Behavior</a></li><li><a href="/cpp/overload-resolution.html" class="sidebar-link">Overload resolution</a></li><li><a href="/cpp/move-semantics.html" class="sidebar-link">Move Semantics</a></li><li><a href="/cpp/pimpl-idiom.html" class="sidebar-link">Pimpl Idiom</a></li><li><a href="/cpp/auto.html" class="sidebar-link">auto</a></li><li><a href="/cpp/copy-elision.html" class="sidebar-link">Copy Elision</a></li><li><a href="/cpp/fold-expressions.html" class="sidebar-link">Fold Expressions</a></li><li><a href="/cpp/unions.html" class="sidebar-link">Unions</a></li><li><a href="/cpp/design-pattern-implementation-in-c.html" class="sidebar-link">Design pattern implementation in C++</a></li><li><a href="/cpp/singleton-design-pattern.html" class="sidebar-link">Singleton Design Pattern</a></li><li><a href="/cpp/user-defined-literals.html" class="sidebar-link">User-Defined Literals</a></li><li><a href="/cpp/memory-management.html" class="sidebar-link">Memory management</a></li><li><a href="/cpp/c-11-memory-model.html" class="sidebar-link">C++11 Memory Model</a></li><li><a href="/cpp/scopes.html" class="sidebar-link">Scopes</a></li><li><a href="/cpp/static-assert.html" class="sidebar-link">static_assert</a></li><li><a href="/cpp/constexpr.html" class="sidebar-link">constexpr</a></li><li><a href="/cpp/one-definition-rule-odr.html" class="sidebar-link">One Definition Rule (ODR)</a></li><li><a href="/cpp/unspecified-behavior.html" class="sidebar-link">Unspecified behavior</a></li><li><a href="/cpp/argument-dependent-name-lookup.html" class="sidebar-link">Argument Dependent Name Lookup</a></li><li><a href="/cpp/attributes.html" class="sidebar-link">Attributes</a></li><li><a href="/cpp/recursion-in-c.html" class="sidebar-link">Recursion in C++</a></li><li><a href="/cpp/arithmitic-metaprogramming.html" class="sidebar-link">Arithmitic Metaprogramming</a></li><li><a href="/cpp/callable-objects.html" class="sidebar-link">Callable Objects</a></li><li><a href="/cpp/client-server-examples.html" class="sidebar-link">Client server examples</a></li><li><a href="/cpp/const-correctness.html" class="sidebar-link">Const Correctness</a></li><li><a href="/cpp/parameter-packs.html" class="sidebar-link">Parameter packs</a></li><li><a href="/cpp/build-systems.html" class="sidebar-link">Build Systems</a></li><li><a href="/cpp/concurrency-with-openmp.html" class="sidebar-link">Concurrency With OpenMP</a></li><li><a href="/cpp/resource-management.html" class="sidebar-link">Resource Management</a></li><li><a href="/cpp/storage-class-specifiers.html" class="sidebar-link">Storage class specifiers</a></li><li><a href="/cpp/linkage-specifications.html" class="sidebar-link">Linkage specifications</a></li><li><a href="/cpp/digit-separators.html" class="sidebar-link">Digit separators</a></li><li><a href="/cpp/c-incompatibilities.html" class="sidebar-link">C incompatibilities</a></li><li><a href="/cpp/side-by-side-comparisons-of-classic-c-examples-solved-via-c-vs-c-11-vs-c-14-vs-c-17.html" class="sidebar-link">Side by Side Comparisons of classic C++ examples solved via C++ vs C++11 vs C++14 vs C++17</a></li><li><a href="/cpp/compiling-and-building.html" class="sidebar-link">Compiling and Building</a></li><li><a href="/cpp/common-compile-linker-errors-gcc.html" class="sidebar-link">Common compile/linker errors (GCC)</a></li><li><a href="/cpp/more-undefined-behaviors-in-c.html" class="sidebar-link">More undefined behaviors in C++</a></li><li><a href="/cpp/unit-testing-in-c.html" class="sidebar-link">Unit Testing in C++</a></li><li><a href="/cpp/c-debugging-and-debug-prevention-tools-techniques.html" class="sidebar-link">C++ Debugging and Debug-prevention Tools & Techniques</a></li><li><a href="/cpp/optimization-in-c.html" class="sidebar-link">Optimization in C++</a></li><li><a href="/cpp/optimization.html" class="sidebar-link">Optimization</a></li><li><a href="/cpp/profiling.html" class="sidebar-link">Profiling</a></li><li><a href="/cpp/refactoring-techniques.html" class="sidebar-link">Refactoring Techniques</a></li><li><a href="/cpp/internationalization-in-c.html" class="sidebar-link">Internationalization in C++</a></li><li><a href="/cpp/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="regular-expressions"><a href="#regular-expressions" class="header-anchor">#</a> Regular expressions</h1> <p><a href="https://en.wikipedia.org/wiki/Regular_expression" target="_blank" rel="noopener noreferrer">Regular Expressions<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> (sometimes called regexs or regexps) are a textual syntax which represents the patterns which can be matched in the strings operated upon.</p> <p>Regular Expressions, introduced in <a href="https://stackoverflow.com/questions/tagged/c%2b%2b11" target="_blank" rel="noopener noreferrer">c++11<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>, may optionally support a return array of matched strings or another textual syntax defining how to replace matched patterns in strings operated upon.</p> <h2 id="basic-regex-match-and-regex-search-examples"><a href="#basic-regex-match-and-regex-search-examples" class="header-anchor">#</a> Basic regex_match and regex_search Examples</h2> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">auto</span> input <span class="token operator">=</span> <span class="token string">"Some people, when confronted with a problem, think \"I know, I'll use regular expressions.\""</span>s<span class="token punctuation">;</span>
smatch sm<span class="token punctuation">;</span>
cout <span class="token operator"><<</span> input <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
<span class="token comment">// If input ends in a quotation that contains a word that begins with "reg" and another word begining with "ex" then capture the preceeding portion of input</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">regex_match</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> sm<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"(.*)\".*\\breg.*\\bex.*\"\\s*$"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token keyword">auto</span> capture <span class="token operator">=</span> sm<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
cout <span class="token operator"><<</span> <span class="token string">'\t'</span> <span class="token operator"><<</span> capture <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">// Outputs: "\tSome people, when confronted with a problem, think\n"</span>
<span class="token comment">// Search our capture for "a problem" or "# problems"</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">regex_search</span><span class="token punctuation">(</span>capture<span class="token punctuation">,</span> sm<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"(a|d+)\\s+problems?"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> <span class="token keyword">auto</span> count <span class="token operator">=</span> sm<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">"a"</span>s <span class="token operator">?</span> <span class="token number">1</span> <span class="token operator">:</span> <span class="token function">stoi</span><span class="token punctuation">(</span>sm<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
cout <span class="token operator"><<</span> <span class="token string">'\t'</span> <span class="token operator"><<</span> count <span class="token operator"><<</span> <span class="token punctuation">(</span>count <span class="token operator">></span> <span class="token number">1</span> <span class="token operator">?</span> <span class="token string">" problems\n"</span> <span class="token operator">:</span> <span class="token string">" problem\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Outputs: "\t1 problem\n"</span>
cout <span class="token operator"><<</span> <span class="token string">"Now they have "</span> <span class="token operator"><<</span> count <span class="token operator">+</span> <span class="token number">1</span> <span class="token operator"><<</span> <span class="token string">" problems.\n"</span><span class="token punctuation">;</span> <span class="token comment">// Ouputs: "Now they have 2 problems\n"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p><a href="http://ideone.com/nSRXEa" target="_blank" rel="noopener noreferrer">Live Example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="regex-iterator-example"><a href="#regex-iterator-example" class="header-anchor">#</a> regex_iterator Example</h2> <p>When processing of captures has to be done iteratively a <code>regex_iterator</code> is a good choice. Dereferencing a <code>regex_iterator</code> returns a <code>match_result</code>. This is great for conditional captures or captures which have interdependence. Let's say that we want to tokenize some C++ code. Given:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token class-name">TOKENS</span> <span class="token punctuation">{</span>
NUMBER<span class="token punctuation">,</span>
ADDITION<span class="token punctuation">,</span>
SUBTRACTION<span class="token punctuation">,</span>
MULTIPLICATION<span class="token punctuation">,</span>
DIVISION<span class="token punctuation">,</span>
EQUALITY<span class="token punctuation">,</span>
OPEN_PARENTHESIS<span class="token punctuation">,</span>
CLOSE_PARENTHESIS
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p>We can tokenize this string: <code>const auto input = "42/2 + -8\t=\n(2 + 2) * 2 * 2 -3"s</code> with a <code>regex_iterator</code> like this:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code>vector<span class="token operator"><</span>TOKENS<span class="token operator">></span> tokens<span class="token punctuation">;</span>
<span class="token keyword">const</span> regex re<span class="token punctuation">{</span> <span class="token string">"\\s*(\\(?)\\s*(-?\\s*\\d+)\\s*(\\)?)\\s*(?:(\\+)|(-)|(\\*)|(/)|(=))"</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token function">for_each</span><span class="token punctuation">(</span><span class="token function">sregex_iterator</span><span class="token punctuation">(</span><span class="token function">cbegin</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cend</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span><span class="token punctuation">,</span> re<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">sregex_iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token operator">&</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">auto</span><span class="token operator">&</span> i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
tokens<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>OPEN_PARENTHESIS<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
tokens<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>i<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string">'-'</span> <span class="token operator">?</span> NEGATIVE_NUMBER <span class="token operator">:</span> NON_NEGATIVE_NUMBER<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
tokens<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>CLOSE_PARENTHESIS<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">auto</span> it <span class="token operator">=</span> <span class="token function">next</span><span class="token punctuation">(</span><span class="token function">cbegin</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> result <span class="token operator">=</span> ADDITION<span class="token punctuation">;</span> it <span class="token operator">!=</span> <span class="token function">cend</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>result<span class="token punctuation">,</span> <span class="token operator">++</span>it<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>it<span class="token operator">-></span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">></span> <span class="token number">0U</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
tokens<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token generic-function"><span class="token function">static_cast</span><span class="token generic class-name"><span class="token operator"><</span>TOKENS<span class="token operator">></span></span></span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
match_results<span class="token operator"><</span>string<span class="token double-colon punctuation">::</span>const_reverse_iterator<span class="token operator">></span> sm<span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">regex_search</span><span class="token punctuation">(</span><span class="token function">crbegin</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">crend</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span><span class="token punctuation">,</span> sm<span class="token punctuation">,</span> regex<span class="token punctuation">{</span> tokens<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> SUBTRACTION <span class="token operator">?</span> <span class="token string">"^\\s*\\d+\\s*-\\s*(-?)"</span> <span class="token operator">:</span> <span class="token string">"^\\s*\\d+\\s*(-?)"</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
tokens<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>sm<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> NON_NEGATIVE_NUMBER <span class="token operator">:</span> NEGATIVE_NUMBER<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p><a href="http://ideone.com/Rv5WNI" target="_blank" rel="noopener noreferrer">Live Example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>A notable gotcha with regex iterators is that the <code>regex</code> argument must be an L-value, an R-value will not work: <a href="http://stackoverflow.com/q/29895747/2642059" target="_blank" rel="noopener noreferrer">Visual Studio regex_iterator Bug?<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="anchors"><a href="#anchors" class="header-anchor">#</a> Anchors</h2> <p>C++ provides only 4 anchors:</p> <ul><li><code>^</code> which asserts the start of the string</li> <li><code>$</code> which asserts the end of the string</li> <li><code>\b</code> which asserts a <code>\W</code> character or the beginning or end of the string</li> <li><code>\B</code> which asserts a <code>\w</code> character</li></ul> <p>Let's say for example we want to capture a number <strong>with</strong> it's sign:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">auto</span> input <span class="token operator">=</span> <span class="token string">"+1--12*123/+1234"</span>s<span class="token punctuation">;</span>
smatch sm<span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">regex_search</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> sm<span class="token punctuation">,</span> regex<span class="token punctuation">{</span> <span class="token string">"(?:^|\\b\\W)([+-]?\\d+)"</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">do</span> <span class="token punctuation">{</span>
cout <span class="token operator"><<</span> sm<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
input <span class="token operator">=</span> sm<span class="token punctuation">.</span><span class="token function">suffix</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token function">regex_search</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> sm<span class="token punctuation">,</span> regex<span class="token punctuation">{</span> <span class="token string">"(?:^\\W|\\b\\W)([+-]?\\d+)"</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p><a href="http://ideone.com/uE4dGr" target="_blank" rel="noopener noreferrer">Live Example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>An important note here is that the anchor does not consume any characters.</p> <h2 id="regex-replace-example"><a href="#regex-replace-example" class="header-anchor">#</a> regex_replace Example</h2> <p>This code takes in various brace styles and converts them to <a href="https://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS" target="_blank" rel="noopener noreferrer">One True Brace Style<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">auto</span> input <span class="token operator">=</span> <span class="token string">"if (KnR)\n\tfoo();\nif (spaces) {\n foo();\n}\nif (allman)\n{\n\tfoo();\n}\nif (horstmann)\n{\tfoo();\n}\nif (pico)\n{\tfoo(); }\nif (whitesmiths)\n\t{\n\tfoo();\n\t}\n"</span>s<span class="token punctuation">;</span>
cout <span class="token operator"><<</span> input <span class="token operator"><<</span> <span class="token function">regex_replace</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"(.+?)\\s*\\{?\\s*(.+?;)\\s*\\}?\\s*"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"$1 {\n\t$2\n}\n"</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
</code></pre></div><p><a href="http://ideone.com/ICR5wM" target="_blank" rel="noopener noreferrer">Live Example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="regex-token-iterator-example"><a href="#regex-token-iterator-example" class="header-anchor">#</a> regex_token_iterator Example</h2> <p>A <a href="http://en.cppreference.com/w/cpp/regex/regex_token_iterator" target="_blank" rel="noopener noreferrer"><code>std::regex_token_iterator</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> provides a tremendous tool for <a href="http://stackoverflow.com/a/28880605/2642059" target="_blank" rel="noopener noreferrer">extracting elements of a Comma Separated Value file<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>. Aside from the advantages of iteration, this iterator is also able to capture escaped commas where other methods struggle:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">auto</span> input <span class="token operator">=</span> <span class="token string">"please split,this,csv, ,line,\\,\n"</span>s<span class="token punctuation">;</span>
<span class="token keyword">const</span> regex re<span class="token punctuation">{</span> <span class="token string">"((?:[^\\\\,]|\\\\.)+)(?:,|$)"</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> vector<span class="token operator"><</span>string<span class="token operator">></span> m_vecFields<span class="token punctuation">{</span> <span class="token function">sregex_token_iterator</span><span class="token punctuation">(</span><span class="token function">cbegin</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cend</span><span class="token punctuation">(</span>input<span class="token punctuation">)</span><span class="token punctuation">,</span> re<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">sregex_token_iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">;</span>
cout <span class="token operator"><<</span> input <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
<span class="token function">copy</span><span class="token punctuation">(</span><span class="token function">cbegin</span><span class="token punctuation">(</span>m_vecFields<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">cend</span><span class="token punctuation">(</span>m_vecFields<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token generic-function"><span class="token function">ostream_iterator</span><span class="token generic class-name"><span class="token operator"><</span>string<span class="token operator">></span></span></span><span class="token punctuation">(</span>cout<span class="token punctuation">,</span> <span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p><a href="http://ideone.com/lySlTJ" target="_blank" rel="noopener noreferrer">Live Example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p>A notable gotcha with regex iterators is, that the <code>regex</code> argument must be an L-value. <a href="http://stackoverflow.com/q/29895747/2642059" target="_blank" rel="noopener noreferrer">An R-value will not work<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>.</p> <h2 id="splitting-a-string"><a href="#splitting-a-string" class="header-anchor">#</a> Splitting a string</h2> <div class="language-cpp extra-class"><pre class="language-cpp"><code>std<span class="token double-colon punctuation">::</span>vector<span class="token operator"><</span>std<span class="token double-colon punctuation">::</span>string<span class="token operator">></span> <span class="token function">split</span><span class="token punctuation">(</span><span class="token keyword">const</span> std<span class="token double-colon punctuation">::</span>string <span class="token operator">&</span>str<span class="token punctuation">,</span> std<span class="token double-colon punctuation">::</span>string regex<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
std<span class="token double-colon punctuation">::</span>regex r<span class="token punctuation">{</span> regex <span class="token punctuation">}</span><span class="token punctuation">;</span>
std<span class="token double-colon punctuation">::</span>sregex_token_iterator start<span class="token punctuation">{</span> str<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> str<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> r<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> end<span class="token punctuation">;</span>
<span class="token keyword">return</span> std<span class="token double-colon punctuation">::</span><span class="token generic-function"><span class="token function">vector</span><span class="token generic class-name"><span class="token operator"><</span>std<span class="token double-colon punctuation">::</span>string<span class="token operator">></span></span></span><span class="token punctuation">(</span>start<span class="token punctuation">,</span> end<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">"Some string\t with whitespace "</span><span class="token punctuation">,</span> <span class="token string">"\\s+"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// "Some", "string", "with", "whitespace"</span>
</code></pre></div><h2 id="quantifiers"><a href="#quantifiers" class="header-anchor">#</a> Quantifiers</h2> <p>Let's say that we're given <code>const string input</code> as a phone number to be validated. We could start by requiring a numeric input with a <strong>zero or more quantifier</strong>: <code>regex_match(input, regex("\\d*"))</code> or a <strong>one or more quantifier</strong>: <code>regex_match(input, regex("\\d+"))</code> But both of those really fall short if <code>input</code> contains an invalid numeric string like: "123" Let's use a <strong>n or more quantifier</strong> to ensure that we're getting at least 7 digits:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token function">regex_match</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"\\d{7,}"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>This will guarantee that we will get at least a phone number of digits, but <code>input</code> could also contain a numeric string that's too long like: "123456789012". So lets go with a <strong>between n and m quantifier</strong> so the <code>input</code> is at least 7 digits but not more than 11:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token function">regex_match</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"\\d{7,11}"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>This gets us closer, but illegal numeric strings that are in the range of [7, 11] are still accepted, like: "123456789" So let's make the country code optional with a <strong>lazy quantifier</strong>:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token function">regex_match</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"\\d?\\d{7,10}"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>It's important to note that the <strong>lazy quantifier</strong> matches <strong>as few characters as possible</strong>, so the only way this character will be matched is if there are already 10 characters that have been matched by <code>\d{7,10}</code>. (To match the first character greedily we would have had to do: <code>\d{0,1}</code>.) The <strong>lazy quantifier</strong> can be appended to any other quantifier.</p> <p>Now, how would we make the area code optional <strong>and</strong> only accept a country code if the area code was present?</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token function">regex_match</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"(?:\\d{3,4})?\\d{7}"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>In this final regex, the <code>\d{7}</code> <strong>requires</strong> 7 digits. These 7 digits are optionally preceded by either 3 or 4 digits.</p> <p>Note that we did not append the <strong>lazy quantifier</strong>: <strike><code>\d{3,4}?\d{7}</code></strike>, the <code>\d{3,4}?</code> would have matched either 3 or 4 characters, preferring 3. Instead we're making the non-capturing group match at most once, preferring not to match. Causing a mismatch if <code>input</code> didn't include the area code like: "1234567".</p> <p>In conclusion of the quantifier topic, I'd like to mention the other appending quantifier that you can use, the <strong>possessive quantifier</strong>. <strong>Either</strong> the <strong>lazy quantifier</strong> or the <strong>possessive quantifier</strong> can be appended to any quantifier. The <strong>possessive quantifier</strong>'s only function is to assist the regex engine by telling it, greedily take these characters <strong>and don't ever give them up even if it causes the regex to fail</strong>. This for example doesn't make much sense: <code>regex_match(input, regex("\\d{3,4}+\\d{7}))</code> Because an <code>input</code> like: "1234567890" wouldn't be matched as <code>\d{3,4}+</code> will always match 4 characters even if matching 3 would have allowed the regex to succeed.<br>
The <strong>possessive quantifier</strong> is best used <strong>when the quantified token limits the number of matchable characters</strong>. For example:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token function">regex_match</span><span class="token punctuation">(</span>input<span class="token punctuation">,</span> <span class="token function">regex</span><span class="token punctuation">(</span><span class="token string">"(?:.*\\d{3,4}+){3}"</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>Can be used to match if <code>input</code> contained any of the following:</p> <blockquote></blockquote> <p>123 456 7890<br>
123-456-7890<br>
(123)456-7890<br>
(123) 456 - 7890</p> <p>But when this regex really shines is when <code>input</code> contains an <strong>illegal</strong> input:</p> <blockquote></blockquote> <p>12345 - 67890</p> <p>Without the <strong>possessive quantifier</strong> the regex engine has to go back and test <strong>every combination of <code>.*</code> and either 3 or 4 characters</strong> to see if it can find a matchable combination. With the <strong>possessive quantifier</strong> the regex starts where the 2<sup>nd</sup> <strong>possessive quantifier</strong> left off, the '0' character, and the regex engine tries to adjust the <code>.*</code> to allow <code>\d{3,4}</code> to match; when it can't the regex just fails, no back tracking is done to see if earlier <code>.*</code> adjustment could have allowed a match.</p> <h4 id="syntax"><a href="#syntax" class="header-anchor">#</a> Syntax</h4> <ul><li>regex_match // Returns whether the entire character sequence was matched by the regex, optionally capturing into a match object</li> <li>regex_search // Returns whether a portion of the character sequence was matched by the regex, optionally capturing into a match object</li> <li>regex_replace // Returns the input character sequence as modified by a regex via a replacement format string</li> <li>regex_token_iterator // Initialized with a character sequence defined by iterators, a list of capture indexes to iterate over, and a regex. Dereferencing returns the currently indexed match of the regex. Incrementing moves to the next capture index or if currently at the last index, resets the index and hinds the next occurrence of a regex match in the character sequence</li> <li>regex_iterator // Initialized with a character sequence defined by iterators and a regex. Dereferencing returns the portion of the character sequence the entire regex currently matches. Incrementing finds the next occurrence of a regex match in the character sequence</li></ul> <h4 id="parameters"><a href="#parameters" class="header-anchor">#</a> Parameters</h4> <table><thead><tr><th>Signature</th> <th>Description</th></tr></thead> <tbody><tr><td><code>bool regex_match(BidirectionalIterator first, BidirectionalIterator last, smatch& sm, const regex& re, regex_constraints::match_flag_type flags)</code></td> <td><strong><code>BidirectionalIterator</code></strong> is any character iterator that provides increment and decrement operators <strong><code>smatch</code></strong> may be <code>cmatch</code> or any other other variant of <code>match_results</code> that accepts the type of <code>BidirectionalIterator</code> the <code>smatch</code> argument may be ommitted if the results of the regex are not needed <strong>Returns</strong> whether <code>re</code> matches the entire character sequence defined by <code>first</code> and <code>last</code></td></tr> <tr><td><code>bool regex_match(const string& str, smatch& sm, const regex re&, regex_constraints::match_flag_type flags)</code></td> <td><strong><code>string</code></strong> may be either a <code>const char*</code> or an L-Value <code>string</code>, <strong>the functions accepting an R-Value <code>string</code> are explicitly deleted</strong> <strong><code>smatch</code></strong> may be <code>cmatch</code> or any other other variant of <code>match_results</code> that accepts the type of <code>str</code> the <code>smatch</code> argument may be ommitted if the results of the regex are not needed <strong>Returns</strong> whether <code>re</code> matches the entire character sequence defined by <code>str</code></td></tr></tbody></table></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/cpp/regular-expressions.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/cpp/iteration.html" class="prev">
Iteration
</a></span> <span class="next"><a href="/cpp/implementation-defined-behavior.html">
Implementation-defined behavior
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/702.1a82db2e.js" defer></script>
</body>
</html>