gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:14:27 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com