python - 如何在 julia 中使用 python regexp 模块

标签 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', '!']

我对 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/

相关文章:

python - 为什么在python中以 'w'模式打开文件时会截断

python - 如何在 Python 中运行使用包的 Julia 文件?

julia - 为什么加号运算符在 Julia 中被矢量化而不是除法运算符?

java - Python 相当于 Java 的 Class.getResource

python - 如何在 python 中规范化二维 numpy 数组?

Python:在迭代时将元素添加到列表中

numpy - 将输入转换为 julia 中的数组

parallel-processing - Julia Parallel 宏似乎不起作用

julia - 使用 `Int((n+1)/2)` 、 `round(Int, (n+1)/2)` 或 `Int((n+1)//2)` 哪个更好?

python - 我怀疑我在 Mac OS X 10.6.3 上安装了多个版本的 Python 2.6;如何设置应启动哪个终端?