如何在julia

时间:2018-01-26 14:14:27

标签: python julia

我想在Julia中使用python中给出的特定正则表达式。为此,我添加了PyCall.jl包。不幸的是,我无法将以下的python代码转换为Julia

Python version

>>> 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',)

1 个答案:

答案 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>