我想在Julia中使用python中给出的特定正则表达式。为此,我添加了PyCall.jl包。不幸的是,我无法将以下的python代码转换为Julia
>>> import re
>>> re.findall(r"[\w']+|[.,!?;]", "Hello, I'm a string!")
['Hello', ',', "I'm", 'a', 'string', '!']
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',)
答案 0 :(得分:2)
您可以使用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>