Skip to content

Commit c369d18

Browse files
committed
overlap option for str_count
1 parent 68e1729 commit c369d18

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/matching.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
$docstring_str_count
33
"""
4-
function str_count(column, pattern::Union{String,Regex})
4+
function str_count(column, pattern::Union{String,Regex}; overlap::Bool=false)
55
if ismissing(column)
66
return (column)
77
end
@@ -11,7 +11,7 @@ function str_count(column, pattern::Union{String,Regex})
1111
end
1212

1313
# Count the number of matches for the regular expression
14-
return length(collect(eachmatch(pattern, column)))
14+
return length(collect(eachmatch(pattern, column, overlap=overlap)))
1515
end
1616

1717
"""
@@ -111,15 +111,15 @@ end
111111
"""
112112
$docstring_str_replace_all
113113
"""
114-
function str_replace_all(column, pattern::Union{String, Regex}, replacement::String)
114+
function str_replace_all(column, pattern::Union{String,Regex}, replacement::String)
115115
if ismissing(column)
116116
return column
117117
end
118118

119119
regex_pattern = isa(pattern, String) ? Regex(pattern) : pattern
120120

121121
column = replace(column, regex_pattern => replacement)
122-
122+
123123
return replace(column, r"\s+" => " ")
124124
end
125125

@@ -191,7 +191,7 @@ end
191191
"""
192192
$docstring_str_starts
193193
"""
194-
function str_starts(string::AbstractString, pattern::Union{AbstractString, Regex}; negate::Bool=false)::Bool
194+
function str_starts(string::AbstractString, pattern::Union{AbstractString,Regex}; negate::Bool=false)::Bool
195195
if ismissing(string)
196196
return (string)
197197
end
@@ -202,13 +202,13 @@ function str_starts(string::AbstractString, pattern::Union{AbstractString, Regex
202202
else
203203
error("Pattern must be either a Regex or an AbstractString.")
204204
end
205-
205+
206206
return negate ? !match_result : match_result
207207
end
208208
"""
209209
$docstring_str_ends
210210
"""
211-
function str_ends(string::AbstractString, pattern::Union{AbstractString, Regex}; negate::Bool=false)::Bool
211+
function str_ends(string::AbstractString, pattern::Union{AbstractString,Regex}; negate::Bool=false)::Bool
212212
if ismissing(string)
213213
return (string)
214214
end
@@ -219,7 +219,7 @@ function str_ends(string::AbstractString, pattern::Union{AbstractString, Regex};
219219
else
220220
error("Pattern must be either a Regex or an AbstractString.")
221221
end
222-
222+
223223
return negate ? !match_result : match_result
224224
end
225225

@@ -278,10 +278,10 @@ end
278278
"""
279279
$docstring_str_extract
280280
"""
281-
function str_extract(input::AbstractString, pattern::Union{String, Regex})
281+
function str_extract(input::AbstractString, pattern::Union{String,Regex})
282282
# Convert pattern to Regex if it's a string
283283
regex_pattern = isa(pattern, String) ? Regex(pattern) : pattern
284-
284+
285285
# Find the first match, return missing if none found
286286
m = match(regex_pattern, input)
287287
return m === nothing ? missing : m.match
@@ -290,14 +290,13 @@ end
290290
"""
291291
$docstring_str_extract_all
292292
"""
293-
function str_extract_all(string::AbstractString, pattern::Union{String, Regex})
293+
function str_extract_all(string::AbstractString, pattern::Union{String,Regex})
294294
# Convert pattern to Regex if it's a string
295295
regex_pattern = isa(pattern, String) ? Regex(pattern) : pattern
296-
296+
297297
# Collect matches
298298
matches = [String(m.match) for m in eachmatch(regex_pattern, string)]
299-
299+
300300
# Return missing if no matches found
301301
return isempty(matches) ? missing : matches
302302
end
303-

0 commit comments

Comments
 (0)