在Julia中拉一个子串

在Julia中拉一个子串,julia,substring,Julia,Substring,我正在处理嵌入在syslog消息中的XML。我使用Python删除之外的信息。因为我在和茱莉亚玩,所以我在想办法做同样的事情。我读过findfirst,但这并不能解决问题。这是示例数据 Datetime host other stuff <xml data and more data>stuff at the end Datetime在结尾处承载其他内容 我想要的只是之间的数据。在Python中,我使用 print(line[line.find(“<“):line.find

我正在处理嵌入在syslog消息中的XML。我使用Python删除
之外的信息。因为我在和茱莉亚玩,所以我在想办法做同样的事情。我读过findfirst,但这并不能解决问题。这是示例数据

Datetime host other stuff <xml data and more data>stuff at the end
Datetime在结尾处承载其他内容
我想要的只是
之间的数据。在Python中,我使用

print(line[line.find(“<“):line.find(“>”)])
打印(第[line.find(“”)行)
朱莉娅身上有什么相似之处吗

短暂性脑缺血发作
Joe

如果您检查文档,它将为您提供正确的用法。在这种情况下,您需要的是
println(第[findfirst(line,”))行)
或者您可以使用正则表达式:

julia> str = "Datetime host other stuff <xml data and more data>stuff at the end"
"Datetime host other stuff <xml data and more data>stuff at the end"

julia> rx = r"<(.*?)>"
r"<(.*?)>"

julia> match(rx, str)[1]
"xml data and more data"
julia>str=“Datetime在末尾托管其他内容”
“Datetime在结尾处承载其他内容”
julia>rx=r“”
r“
julia>匹配(rx,str)[1]
“xml数据和更多数据”
如果您想使用Oscar提出的方法,那么正确的语法应该是:

julia> chop(str[findfirst('<',str):findfirst('>',str)], head=1, tail=1)
"xml data and more data"
julia>chop(str[findfirst(“”,str)],head=1,tail=1)
“xml数据和更多数据”
最后请注意,在Python中,您的代码在生成时并没有提供所需的内容:

>>> line = "Datetime host other stuff <xml data and more data>stuff at the end"
>>> print(line[line.find("<"):line.find(">")])
<xml data and more data
>line=“Datetime在末尾托管其他内容”
>>>打印(第[line.find(“”)行)

由于是日志处理,性能可能有点重要。 在这种情况下,使用
子字符串{String}
(不进行内存复制)。 此外,在搜索
“>”
时,您可能希望使用
findlast

SubString(line, findfirst('<', line), findlast('>',line))
子字符串(行,findfirst(“”,行))

这是非复制,返回一个
子字符串{String}
对象。

作为旁注
匹配
返回一个
子字符串
。另外说到速度,我认为值得注意的是寻找
”这个问题有点含糊不清。如果在Stefan所做的更改使regexp变懒之前发生
,则编写的Python代码返回空字符串。如果字符串中有多个
字符,则差异会很明显。