我想在 Julia 中使用 python 中给出的特定正则表达式。为此,我添加了 PyCall.jl 包。不幸的是,我无法将以下 python 代码转换为 Julia
Python version :
>>> import re
>>> re.findall(r"[\w']+|[.,!?;]", "Hello, I'm a string!")
['Hello', ',', "I'm", 'a', 'string', '!']
我对 Julia 的试用:
using PyCall
@pyimport re
re.findall(r"[\w']+|[.,!?;]", "Hello, I'm a string!")
我收到一条错误消息:
ERROR: PyError (ccall(@pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, arg, C_NULL)) <type 'exceptions.TypeError'>
TypeError('first argument must be string or compiled pattern',)
最佳答案
您可以使用Base.@raw_str
non standard string literal macro :
julia> using PyCall: @pyimport
julia> @pyimport re
julia> regex = raw"[\w']+|[.,!?;]"
"[\\w']+|[.,!?;]"
julia> re.findall(regex, "Hello, I'm a string!")
6-element Array{String,1}:
"Hello"
","
"I'm"
"a"
"string"
"!"
julia>
关于python - 如何在 julia 中使用 python regexp 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48463080/