探索Julia(part9)--字符串处理

学习笔记,仅供参考,有错必纠

参考自:Julia数据科学应用–Zacharias Voulgaris

使用Julia-1.1.1



字符串处理


split与join


split


语法:

split(str::AbstractString; limit::Integer=0, keepempty::Bool=false)

输入:

x = "Julia Python R"
show(split(x, " "))

输出:

SubString{String}["Julia", "Python", "R"]

join


语法:

join([io::IO,] strings [, delim [, last]])

将字符串数组连接为单个字符串,在相邻字符串之间插入给定的分隔符(如果有的话)。如果last是给定的,那么最后两个字符串之间将使用它而不是delim。如果给定了io,结果将被写入io,而不是作为字符串返回。


输入:

a = [1, "hjk", true, 'c']
println(join(a))
println(join(a, "+", " and "))

输出:

1hjktruec
1+hjk+true and c

正则表达式


match与eachmatch


match语法:

match(r::Regex, s::AbstractString[, idx::Integer[, addopts]])

在s中搜索正则表达式r的第一个匹配项,并返回包含匹配项的RegexMatch对象,如果匹配失败则不返回任何内容。可以通过访问m.match来检索匹配的子字符串,可以通过m.captures来捕获序列,可选的idx参数指定开始搜索的索引。


eachmatch语法:

eachmatch(r::Regex, s::AbstractString; overlap::Bool=false)

在s中通过正则表达式r搜索所有匹配项,并在匹配项上返回一个迭代器。


输入:

p = r"([A-Z])\w+"
s1 = "Python R Julia"
m = match(p, s1)
println(m)
println(m.match, " ", m.offset)
for m in eachmatch(p, s1)
    println(m.match, "--", m.offset)
end

输出:

RegexMatch("Python", 1="P")
Python 1
Python--1
Julia--10

猜你喜欢

转载自blog.csdn.net/m0_37422217/article/details/107394597