Skip to content

Commit a03639f

Browse files
author
José Valim
committed
Deprecate Regex.index/2
1 parent e7b1242 commit a03639f

File tree

2 files changed

+7
-23
lines changed

2 files changed

+7
-23
lines changed

lib/elixir/lib/regex.ex

+7-13
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,12 @@ defmodule Regex do
7979
end
8080
end
8181

82-
@doc """
83-
Runs the regular expression against the given string
84-
and returns the index (zero indexes) where the first
85-
match occurs, nil otherwise.
86-
87-
## Examples
88-
89-
iex> Regex.index(%r/c(d)/, "abcd")
90-
2
91-
iex> Regex.index(%r/e/, "abcd")
92-
nil
93-
94-
"""
82+
@doc false
9583
def index(regex(re_pattern: compiled), string) do
84+
IO.puts "Regex.index(re, string) is deprecated. Please use " <>
85+
"Regex.run(re, string, return: :index) instead."
86+
Exception.print_stacktrace
87+
9688
case :re.run(string, compiled, [{ :capture, :first, :index }]) do
9789
:nomatch -> nil
9890
{ :match, [{index,_}] } -> index
@@ -125,6 +117,8 @@ defmodule Regex do
125117
["cd", "d"]
126118
iex> Regex.run(%r/e/, "abcd")
127119
nil
120+
iex> Regex.run(%r/c(d)/, "abcd", return: :index)
121+
[{2,2},{3,1}]
128122
129123
"""
130124
def run(regex, string, options // [])

lib/elixir/test/elixir/regex_test.exs

-10
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@ defmodule Regex.BinaryTest do
9393
assert Regex.run(%r"e", "abcd", return: :index) == nil
9494
end
9595
96-
test :index do
97-
assert Regex.index(%r"c(d)", "abcd") == 2
98-
assert Regex.index(%r"e", "abcd") == nil
99-
end
100-
10196
test :scan do
10297
assert Regex.scan(%r"c(d|e)", "abcd abce") == [["d"], ["e"]]
10398
assert Regex.scan(%r"c(?:d|e)", "abcd abce") == ["cd", "ce"]
@@ -183,11 +178,6 @@ defmodule Regex.ListTest do
183178
assert Regex.run(%r"c(d)", "abcd", return: :binary) == ["cd", "d"]
184179
end
185180

186-
test :index do
187-
assert Regex.index(%r'c(d)', 'abcd') == 2
188-
assert Regex.index(%r'e', 'abcd') == nil
189-
end
190-
191181
test :indexes do
192182
assert Regex.run(%r'c(d)', 'abcd', return: :index) == [{2,2},{3,1}]
193183
assert Regex.run(%r'e', 'abcd', return: :index) == nil

0 commit comments

Comments
 (0)