蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

文本实例:读取方向代码和捕获偏移

JULIAJulia 1.10.13 官方便携版,Windows x64,本机隔离目录text-regex.jl
text-regex.jl55 行

include(joinpath(@__DIR__, "observations.jl"))

heading = r"^\h*(?:#|$)"
probe("regex_literal", raw"^\h*(?:#|$)", (typeof(heading),typeof(Regex("^Track "*string(7))),occursin(Regex("^Track "*string(7)),"Track 7 north")),
      (Regex,Regex,true))
probe("regex_occurs", ("station"," # station","  "), map(x -> occursin(heading,x),("station"," # station","  ")),
      (false,true,true))
m = match(heading," # station")
classify(line) = match(heading,line) === nothing ? "data" : "heading"
probe("regex_match", ("station"," # station"), (match(heading,"station"),m.match,classify("station"),classify(" # station")),
      (nothing," #","data","heading"))
text = "sea/N-7 N-21"
probe("regex_capture_text", "# wind report ",
      only(match(r"^\s*#\s*(.*?)\s*$","# wind report ").captures), "wind report")
probe("regex_start_offset", (text,1,8,13),
      (match(r"\d+",text,1).match,match(r"\d+",text,8).match,match(r"\d+",text,13)), ("7","21",nothing))
full = match(r"(N)(-)?(\d+)", "N-21")
short = match(r"(N)(-)?(\d+)", "海N21")
probe("regex_field_match", ("N-21","海N21"), (full.match,short.match),("N-21","N21"))
probe("regex_field_captures", ("N-21","海N21"), (full.captures,short.captures), (["N","-","21"],["N",nothing,"21"]))
probe("regex_field_offset", ("N-21","海N21"), (full.offset,short.offset),(1,4))
probe("regex_field_offsets", ("N-21","海N21"), (full.offsets,short.offsets),([1,2,3],[4,0,5]))
p1,p2,p3 = short
probe("regex_destructure", short.match, (p1,p2,p3),("N",nothing,"21"))
named = match(r"(?<site>[NS])-(?<count>\d+)","S-18")
probe("regex_named_capture", "S-18", (named[:site],named[:count],named[2]), ("S","18","18"))

samples = "海N-7 N-21"
items = collect(eachmatch(r"N-(\d+)",samples))
probe("regex_eachmatch", samples,
      ([(m.match,m.offset,only(m.offsets)) for m in items],collect(m.match for m in eachmatch(r"\d+",samples)),
       length(collect(eachmatch(r"ana","banana"))),length(collect(eachmatch(r"ana","banana";overlap=true)))),
      ([("N-7",4,6),("N-21",8,10)],["7","21"],1,2))
probe("regex_replace", "a7b8c9", (replace("a7b8c9",r"\d"=>"#"),replace("a7b8c9",r"\d"=>"#";count=1)),
      ("a#b#c#","a#b8c9"))
probe("regex_substitute", ("north bay","Q","2026-07-18"),
      (replace("north bay",r"(\w+) (?<zone>\w+)"=>s"\g<zone> \1"),
       replace("Q",r"."=>s"\g<0>!"),
       replace("2026-07-18",r"(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})"=>s"\g<d>/\g<m>/\g<y>")),
      ("bay north","Q!","18/07/2026"))

probe("regex_i", "NORTH", (occursin(r"^north$","NORTH"),occursin(r"^north$"i,"NORTH")),(false,true))
probe("regex_m", "head\nnorth\ntail", (occursin(r"^north$","head\nnorth\ntail"),
      occursin(r"^north$"m,"head\nnorth\ntail")),(false,true))
probe("regex_s", "n\nh", (occursin(r"n.h","n\nh"),occursin(r"n.h"s,"n\nh")),(false,true))
pattern = r"""
  ([NS])  # site direction
  -       # separator
  (\d+)   # count
  """x
probe("regex_x", "N-37", match(pattern,"N-37").captures,["N","37"])
combined = match(r"^north.*south$"ims,"hdr\nNORTH\nsouth\nend")
probe("regex_combined", "hdr\\nNORTH\\nsouth\\nend", combined.match,"NORTH\nsouth")
本例需在 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Julia 1.10.13 官方便携版,Windows x64,本机隔离目录

入口文件:projects/julia-math-text/text-regex.jl

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 text-regex.jl,或下载右侧课程工程包。
  2. 确认本地已准备 Julia 1.10.13 官方便携版,Windows x64,本机隔离目录。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Julia

课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202612a205.zip

下载课程关联工程包:Julia

课程关联工程包(登记关联3章) · 含当前章节或实例 · mbskycom202612a211.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习