如何在 Julia 中使用包装函数替换字符串
How to replace a string using a wrapper function in Julia
我正在尝试编写一个 Julia 函数,该函数使用正则表达式对字符串数组中的每个元素执行查找和替换操作。它本质上是广播 replace()
调用的包装器。如果您熟悉 R 的 stringr 包,这个函数应该或多或少地与 stringr::str_replace_all()
.
相同
下面的代码将 'ee' 的所有实例替换为 'EE',将“greetings”替换为“grEEtings”:
arr = ["hi", "hello", "welcome", "greetings"]
replace.(arr, r"e{2}" => "EE")
我写的函数没有return修改arr
中的值:
function str_replace_all(string::String, pattern::String, replacement::String)
replace.(string, Regex(pattern) => replacement)
end
str_replace_all(arr, "e{2}", "EE")
出了什么问题?谢谢!
在你的函数中删除类型注释,它应该可以工作:
julia> arr = ["hi", "hello", "welcome", "greetings"]
julia> function str_replace_all(string, pattern, replacement)
replace.(string, Regex(pattern) => replacement)
end
您提供的代码还没有 运行,因为您正试图传递一个字符串,但您的函数需要一个字符串数组。您要么必须:
去掉函数定义中string::String
的类型注解,或者
vectorize 您的函数在调用它时使用“.”语法:str_replace_all.(arr, "e{2}", "EE")
还应注意,您的函数不会替换数组中的字符串,它会创建一个包含替换值的新数组。如果你想在你的数组中获取替换值,你可以做类似的事情(编辑:正如DNF在评论中指出的那样,这样一个新数组被创建并绑定到变量arr
,这不是
真的很高效):
function str_replace_all(string, pattern::String, replacement::String)
replace.(string, Regex(pattern) => replacement)
end
arr = str_replace_all(arr, "e{2}", "EE")
你必须实际修改数组:
function str_replace_all!(arr, pattern, replacement)
arr .= replace.(arr, pattern => replacement)
end
你应该在函数名的末尾放一个 !
来表示它改变了输入。这只是约定俗成,没有任何作用,但还是要遵守。
结合大家的回答反馈后,这是我的解决方案:
function str_replace_all(string::String, pattern::String, replacement::String)
replace(string, pattern => replacement)
end
function str_replace_all(string::String, pattern::Regex, replacement::String)
replace(string, pattern => replacement)
end
最好利用多重分派来允许用户决定发送到 replace()
的模式类型——完全匹配(字符串)或正则表达式。此外,解决方案的矢量化应该在调用函数时完成(例如 str_replace_all.()
),而不是放在函数体内(就像在 OP 中调用 replace.()
的情况一样)。
例如:
arr = ["hi", "hello", "welcome", "greetings"]
str_replace_all.(arr, "ee", "EE")
str_replace_all.(arr, r"e{2}", "EE")
我正在尝试编写一个 Julia 函数,该函数使用正则表达式对字符串数组中的每个元素执行查找和替换操作。它本质上是广播 replace()
调用的包装器。如果您熟悉 R 的 stringr 包,这个函数应该或多或少地与 stringr::str_replace_all()
.
下面的代码将 'ee' 的所有实例替换为 'EE',将“greetings”替换为“grEEtings”:
arr = ["hi", "hello", "welcome", "greetings"]
replace.(arr, r"e{2}" => "EE")
我写的函数没有return修改arr
中的值:
function str_replace_all(string::String, pattern::String, replacement::String)
replace.(string, Regex(pattern) => replacement)
end
str_replace_all(arr, "e{2}", "EE")
出了什么问题?谢谢!
在你的函数中删除类型注释,它应该可以工作:
julia> arr = ["hi", "hello", "welcome", "greetings"]
julia> function str_replace_all(string, pattern, replacement)
replace.(string, Regex(pattern) => replacement)
end
您提供的代码还没有 运行,因为您正试图传递一个字符串,但您的函数需要一个字符串数组。您要么必须:
去掉函数定义中
string::String
的类型注解,或者vectorize 您的函数在调用它时使用“.”语法:
str_replace_all.(arr, "e{2}", "EE")
还应注意,您的函数不会替换数组中的字符串,它会创建一个包含替换值的新数组。如果你想在你的数组中获取替换值,你可以做类似的事情(编辑:正如DNF在评论中指出的那样,这样一个新数组被创建并绑定到变量arr
,这不是
真的很高效):
function str_replace_all(string, pattern::String, replacement::String)
replace.(string, Regex(pattern) => replacement)
end
arr = str_replace_all(arr, "e{2}", "EE")
你必须实际修改数组:
function str_replace_all!(arr, pattern, replacement)
arr .= replace.(arr, pattern => replacement)
end
你应该在函数名的末尾放一个 !
来表示它改变了输入。这只是约定俗成,没有任何作用,但还是要遵守。
结合大家的回答反馈后,这是我的解决方案:
function str_replace_all(string::String, pattern::String, replacement::String)
replace(string, pattern => replacement)
end
function str_replace_all(string::String, pattern::Regex, replacement::String)
replace(string, pattern => replacement)
end
最好利用多重分派来允许用户决定发送到 replace()
的模式类型——完全匹配(字符串)或正则表达式。此外,解决方案的矢量化应该在调用函数时完成(例如 str_replace_all.()
),而不是放在函数体内(就像在 OP 中调用 replace.()
的情况一样)。
例如:
arr = ["hi", "hello", "welcome", "greetings"]
str_replace_all.(arr, "ee", "EE")
str_replace_all.(arr, r"e{2}", "EE")