@@ -163,14 +163,11 @@ polynomial time.
163163Examples
164164********
165165
166- Captures
167- #################
166+ Match
167+ #####
168168
169- Like most other regex engines, this library only
170- captures the last repetition in a repeated group
171- (``*``, ``+``, ``{n}``). The space complexity for
172- captures is ``O(regex_len * groups_count)``, and so
173- it can be used to match untrusted text.
169+ The ``match`` function match a text from start to end, similar to ``^regex$``.
170+ This means the whole text needs to match the regex for this function to return ``true``.
174171
175172.. code-block:: nim
176173 :test:
@@ -182,31 +179,53 @@ it can be used to match untrusted text.
182179 else:
183180 doAssert false, "no match"
184181
185- To check if a capture group didn't match you can use ``reNonCapture``.
186- For example ``doAssert m.group(0) == reNonCapture``.
182+ Captures
183+ ########
187184
188- Verbose Mode
189- ############
185+ Like most other regex engines, this library only
186+ captures the last repetition in a repeated group
187+ (``*``, ``+``, ``{n}``). Note how in the previous example
188+ both ``styleCheck:hint`` and ``colors:off`` are matched in
189+ the same group but only the last captured match (``colors:off``)
190+ is returned.
190191
191- Verbose mode `(?x)` makes regexes more readable by allowing
192- comments and multi-lines within the regular expression
193- itself. The caveat is spaces and pound signs must be
194- scaped to be matched.
192+ To check if a capture group did match you can use ``reNonCapture``.
193+ For example ``doAssert m.group(0) != reNonCapture``. This is useful
194+ to disambiguate empty captures and non-matched captures. Since both return
195+ an empty string when slicing the text.
196+
197+ The space complexity for captures is ``O(regex_len * groups_count)``,
198+ and so it can be used to match untrusted text.
199+
200+ Find
201+ ####
202+
203+ The ``find`` function will find the first piece of text that
204+ match a given regex.
195205
196206.. code-block:: nim
197207 :test:
198- const exp = re2"""(?x)
199- \# # the hashtag
200- \w+ # hashtag words
208+ let text = """
209+ The Continental's email list:
210+ john_wick@continental.com
211+ winston@continental.com
212+ ms_perkins@continental.com
201213 """
202- let text = "#NimLang"
203- doAssert match(text, exp)
214+ var match = ""
215+ var capture = ""
216+ var m: RegexMatch2
217+ if find(text, re2"(\w+)@\w+\.\w+", m):
218+ match = text[m.boundaries]
219+ capture = text[m.group(0)]
220+ doAssert match == "john_wick@continental.com"
221+ doAssert capture == "john_wick"
204222
205223Find All
206224########
207225
208- The `findAll` function will find all boundaries
209- and captures that match the regular expression.
226+ The `findAll` function will find all pieces of text
227+ that match a given regex, returning their boundaries
228+ and captures/submatches.
210229
211230.. code-block:: nim
212231 :test:
@@ -228,6 +247,23 @@ and captures that match the regular expression.
228247 ]
229248 doAssert captures == @["john_wick", "winston", "ms_perkins"]
230249
250+ Verbose Mode
251+ ############
252+
253+ Verbose mode `(?x)` makes regexes more readable by allowing
254+ comments and multi-lines within the regular expression
255+ itself. The caveat is spaces and pound signs must be
256+ scaped to be matched.
257+
258+ .. code-block:: nim
259+ :test:
260+ const exp = re2"""(?x)
261+ \# # the hashtag
262+ \w+ # hashtag words
263+ """
264+ let text = "#NimLang"
265+ doAssert match(text, exp)
266+
231267Match Macro
232268###########
233269
0 commit comments