Julia之初体验(九)字符串连接与匹配

 

串联是最常见和有用的字符串操作之一:

julia> greet = "Hello"
"Hello"

julia> whom = "world"
"world"

julia> string(greet, ", ", whom, ".\n")
"Hello, world.\n"
julia> a, b = "\xe2\x88", "\x80"
("\xe2\x88", "\x80")

julia> c = a*b
"∀"

julia> collect.([a, b, c])
3-element Array{Array{Char,1},1}:
 ['\xe2\x88']
 ['\x80']
 ['∀']

julia> length.([a, b, c])
3-element Array{Int64,1}:
 1
 1
 1

 

julia> greet * ", " * whom * ".\n"
"Hello, world.\n"

 

插补
但是,使用串联构造字符串可能会变得有些麻烦。 为了减少对字符串或重复乘法的这些冗长调用的需要,Julia允许使用$内插到字符串文字中,如Perl中所示:

注意:这个很有用!!!用$符!!

julia> "$greet, $whom.\n"
"Hello, world.\n"
julia> "1 + 2 = $(1 + 2)"
"1 + 2 = 3"
julia> v = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> "v: $v"
"v: [1, 2, 3]"
julia> c = 'x'
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

julia> "hi, $c"
"hi, x"
julia> print("I have \$100 in my account.\n")#注意这里用转义符
I have $100 in my account.

三重引用的字符串文字
使用三引号(“”“ ...”“”)创建字符串时,它们具有一些特殊的行为,这些行为对于创建更长的文本块很有用。

首先,三引号的字符串也要缩进最小缩进线的水平。 这对于在缩进的代码中定义字符串很有用。 例如:

julia> str = """
           Hello,
           world.
         """
"  Hello,\n  world.\n"
julia> """    This
         is
           a test"""
"    This\nis\n  a test"
julia> """hello"""
"hello"

julia> """
       hello"""
"hello"

julia> """

       hello"""
"\nhello"
julia> """
         Hello,
         world."""
"Hello,\nworld."

字符操作

julia> "abracadabra" < "xylophone"
true

julia> "abracadabra" == "xylophone"
false

julia> "Hello, world." != "Goodbye, world."
true

julia> "1 + 2 = 3" == "1 + 2 = $(1 + 2)"
true

可以使用findfirst和findlast函数搜索特定字符的索引:

julia> findfirst(isequal('o'), "xylophone")
4

julia> findlast(isequal('o'), "xylophone")
7

julia> findfirst(isequal('z'), "xylophone")

可以使用findnext和findprev函数以给定的偏移量开始搜索字符:

julia> findnext(isequal('o'), "xylophone", 1)
4

julia> findnext(isequal('o'), "xylophone", 5)
7

julia> findprev(isequal('o'), "xylophone", 5)
4

julia> findnext(isequal('o'), "xylophone", 8)

可以使用occursin函数检查在字符串中是否找到子字符串:

julia> occursin("world", "Hello, world.")
true

julia> occursin("o", "Xylophon")
true

julia> occursin("a", "Xylophon")
false

julia> occursin('o', "Xylophon")
true

最后一个例子表明,existsin也可以查找字符文字。

另外两个方便的字符串函数是repeat和join:

julia> repeat(".:Z:.", 10)
".:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:..:Z:."

julia> join(["apples", "bananas", "pineapples"], ", ", " and ")
"apples, bananas and pineapples"

     firstindex(str)给出可用于索引到str的最小(字节)索引(对于字符串,始终为1,对于其他容器,不一定为true)。
     lastindex(str)给出可用于索引str的最大(字节)索引。
     length(str)str中的字符数。
     length(str,i,j)从i到j的str中有效字符索引的数量。
     ncodeunits(str)字符串中的代码单元数。
     codeunit(str,i)在索引为i的字符串str中给出代码单位值。
     给字符串中的任意索引的thisind(str,i)找到该索引指向的字符的第一个索引。
     nextind(str,i,n = 1)查找从索引i开始的第n个字符的开始。
     prevind(str,i,n = 1)查找从索引i开始的第n个字符的开始。

正则表达式

Julia具有PCRE库提供的与Perl兼容的正则表达式(regexes)(有关语法的说明,请参见此处)。 正则表达式通过两种方式与字符串相关:明显的联系是,正则表达式用于查找字符串中的正则模式。 另一个联系是,正则表达式本身是作为字符串输入的,它们被解析为状态机,可用于有效地搜索字符串中的模式。 在Julia中,使用以r开头的各种标识符作为前缀的非标准字符串文字输入正则表达式。 没有打开任何选项的最基本的正则表达式文字只使用r“ ...”:

julia> r"^\s*(?:#|$)"
r"^\s*(?:#|$)"

julia> typeof(ans)
Regex

要检查正则表达式是否与字符串匹配:

julia> occursin(r"^\s*(?:#|$)", "not a comment")
false

julia> occursin(r"^\s*(?:#|$)", "# a comment")
true

match函数

julia> match(r"^\s*(?:#|$)", "not a comment")

julia> match(r"^\s*(?:#|$)", "# a comment")
RegexMatch("#")
m = match(r"^\s*(?:#|$)", line)
if m === nothing
    println("not a comment")
else
    println("blank or comment")
end
julia> m = match(r"^\s*(?:#\s*(.*?)\s*$|$)", "# a comment ")
RegexMatch("# a comment ", 1="a comment")
julia> m = match(r"[0-9]","aaaa1aaaa2aaaa3",1)
RegexMatch("1")

julia> m = match(r"[0-9]","aaaa1aaaa2aaaa3",6)
RegexMatch("2")

julia> m = match(r"[0-9]","aaaa1aaaa2aaaa3",11)
RegexMatch("3")
julia> m = match(r"(a|b)(c)?(d)", "acd")
RegexMatch("acd", 1="a", 2="c", 3="d")

julia> m.match
"acd"

julia> m.captures
3-element Array{Union{Nothing, SubString{String}},1}:
 "a"
 "c"
 "d"

julia> m.offset
1

julia> m.offsets
3-element Array{Int64,1}:
 1
 2
 3

julia> m = match(r"(a|b)(c)?(d)", "ad")
RegexMatch("ad", 1="a", 2=nothing, 3="d")

julia> m.match
"ad"

julia> m.captures
3-element Array{Union{Nothing, SubString{String}},1}:
 "a"
 nothing
 "d"

julia> m.offset
1

julia> m.offsets
3-element Array{Int64,1}:
 1
 0
 2

 

julia> first, second, third = m.captures; first
"a"
julia> m=match(r"(?<hour>\d+):(?<minute>\d+)","12:45")
RegexMatch("12:45", hour="12", minute="45")

julia> m[:minute]
"45"

julia> m[2]
"45"
julia> replace("first second", r"(\w+) (?<agroup>\w+)" => s"\g<agroup> \1")
"second first"
julia> replace("a", r"." => s"\g<0>1")
"a1"

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bowen2006

你的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值