@@ -202,21 +202,23 @@ defmodule Regex do
202
202
"""
203
203
@ spec compile ( binary , binary | [ term ] ) :: { :ok , t } | { :error , term }
204
204
def compile ( source , opts \\ "" ) when is_binary ( source ) do
205
- do_compile ( source , opts )
205
+ do_compile ( source , opts , false )
206
206
end
207
207
208
- defp do_compile ( source , opts ) when is_binary ( opts ) do
208
+ defp do_compile ( source , opts , export? ) when is_binary ( opts ) do
209
209
case translate_options ( opts , [ ] ) do
210
210
{ :error , rest } ->
211
211
{ :error , { :invalid_option , rest } }
212
212
213
213
translated_opts ->
214
- do_compile ( source , translated_opts )
214
+ do_compile ( source , translated_opts , export? )
215
215
end
216
216
end
217
217
218
- defp do_compile ( source , opts ) when is_list ( opts ) do
219
- case :re . compile ( source , opts ) do
218
+ defp do_compile ( source , opts , export? ) when is_list ( opts ) do
219
+ compile_opts = if export? , do: [ :export | opts ] , else: opts
220
+
221
+ case :re . compile ( source , compile_opts ) do
220
222
{ :ok , re_pattern } ->
221
223
{ :ok , % Regex { re_pattern: re_pattern , source: source , opts: opts } }
222
224
@@ -225,6 +227,11 @@ defmodule Regex do
225
227
end
226
228
end
227
229
230
+ @ spec compile_export ( binary , binary | [ term ] ) :: { :ok , t } | { :error , term }
231
+ def compile_export ( source , opts \\ "" ) when is_binary ( source ) do
232
+ do_compile ( source , opts , true )
233
+ end
234
+
228
235
@ doc """
229
236
Compiles the regular expression and raises `Regex.CompileError` in case of errors.
230
237
"""
@@ -236,6 +243,17 @@ defmodule Regex do
236
243
end
237
244
end
238
245
246
+ @ doc """
247
+ Compiles the regular expression and raises `Regex.CompileError` in case of errors.
248
+ """
249
+ @ spec compile_export! ( binary , binary | [ term ] ) :: t
250
+ def compile_export! ( source , options \\ "" ) when is_binary ( source ) do
251
+ case compile_export ( source , options ) do
252
+ { :ok , regex } -> regex
253
+ { :error , { reason , at } } -> raise Regex.CompileError , "#{ reason } at position #{ at } "
254
+ end
255
+ end
256
+
239
257
@ doc """
240
258
Recompiles the existing regular expression if necessary.
241
259
@@ -506,6 +524,7 @@ defmodule Regex do
506
524
"""
507
525
@ spec names ( t ) :: [ String . t ( ) ]
508
526
def names ( % Regex { re_pattern: re_pattern } ) do
527
+ re_pattern = maybe_import_pattern ( re_pattern )
509
528
{ :namelist , names } = :re . inspect ( re_pattern , :namelist )
510
529
names
511
530
end
@@ -576,10 +595,15 @@ defmodule Regex do
576
595
% Regex { source: source , opts: compile_opts } = regex
577
596
:re . run ( string , source , compile_opts ++ options )
578
597
else
579
- _ -> :re . run ( string , re_pattern , options )
598
+ _ -> :re . run ( string , maybe_import_pattern ( re_pattern ) , options )
580
599
end
581
600
end
582
601
602
+ defp maybe_import_pattern ( { :re_exported_pattern , _ , _ , _ , _ } = exported ) ,
603
+ do: :re . import ( exported )
604
+
605
+ defp maybe_import_pattern ( re_pattern ) , do: re_pattern
606
+
583
607
@ typedoc """
584
608
Options for regex functions that capture matches.
585
609
"""
0 commit comments