【问题标题】:Regex find, move, and replace正则表达式查找、移动和替换
【发布时间】:2014-10-10 22:08:32
【问题描述】:

我有很多以 yaml 模式开头的降价文件,例如:

---  
title: Miniarchi  
postheaderimage:  
  -  
    img: Miniarchi-1.jpg  
    alt: Minimal Architecture  
class: center  
categories:  
  - Design  
tags:  
  - Architecture  
  - Illustration  
  - Miniarchi  
---  

我将要处理并希望在所有文件中修改的部分是:

postheaderimage:  
  -  
    img: Miniarchi-1.jpg  
    alt: Minimal Architecture  

我想要的样子是:

postheaderimage: Miniarchi-1.jpg  
alt: Minimal Architecture  

我已经想到了我认为应该如何完成的逻辑(使用正则表达式),但我不知道如何实现它。这是我想出的:

  • 找到\w+(-\w+)+.jpg [匹配图像文件名,即本例中的Miniarchi-1.jpg]
  • 存储在变量 $img 中
  • 找到postheaderimage:
  • 替换为 postheaderimage: $img
  • find alt:\s\w.* [匹配图像 alt,即本例中的 Minimal Architecture]
  • 存储在变量 $alt 中
  • 找到\s{2}-\n\s{4}img:\s.{1,}\n\s{4}alt:\s.{1,} [匹配postheaderimage下的所有内容:]
  • 替换为 $alt

我认为这可以使用 applescript 甚至 php 来解决,但我不知道如何。
如果需要我没有提到的更多信息,请询问,我很乐意澄清。

【问题讨论】:

  • 如果你想处理用计算机语言编写的数据,你不需要一个正则表达式,而是一个解析器。对于 YML,有很多解析器,所以只需获取其中一个来处理您的文件。
  • 你为什么不直接找到 img: 并替换为 postheaderimage
  • @Steve 谢谢。没这么想过。

标签: php regex applescript


【解决方案1】:

你要求一个 Applescript,用真正的纯 Applescript 解决这个任务很有趣:

set my_test_file to (path to desktop as string) & "TEST.txt"
tidyUpMyYamlFile(my_test_file)

on tidyUpMyYamlFile(yaml_path)
    -- accessing the file's contents, storing it to fh_content
    try
        set fh to open for access yaml_path
        set fh_content to (read fh)
        close access fh
    on error
        try
            close access fh
        end try
        return
    end try

    -- repairing the postheaderimage part 
    set otid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "postheaderimage:  
  -  
    img:"

    set textParts to text items of fh_content
    set AppleScript's text item delimiters to "postheaderimage:"
    set new_fh_content to textParts as text
    set AppleScript's text item delimiters to otid

    set fh_content_Lines to paragraphs of new_fh_content

    -- repairing the indentation for the word alt: (after the postheaderimage-line only)
    repeat with lineNo from 1 to count fh_content_Lines
        set altOffset to offset of "alt:" in (item lineNo of fh_content_Lines)
        -- this line contains the word "alt:"
        if altOffset > 0 then
            try
                -- the line before starts with the word "postheaderimage:"
                if item (lineNo - 1) of fh_content_Lines starts with "postheaderimage:" then
                    set oldLineContent to item lineNo of fh_content_Lines
                    set item lineNo of fh_content_Lines to text altOffset thru -1 of oldLineContent
                end if
            end try
        end if
    end repeat

    -- building new file content, line endings defined by line feed (Ascii 10)
    set AppleScript's text item delimiters to ASCII character 10
    set new_fh_content to fh_content_Lines as text
    set AppleScript's text item delimiters to otid

    -- writing the new content to the old file
    set fh to open for access yaml_path with write permission
    set eof fh to 0
    write new_fh_content to fh
    close access fh

end tidyUpMyYamlFile

相信我,它真的很快!请尝试复制!该脚本会覆盖源文件!

【讨论】:

  • 感谢@ShooTerKo,但我接受了史蒂夫的建议并进行了修改。再次感谢。
  • 没问题,这是一次很好的培训和乐趣:-)
猜你喜欢
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 2011-06-16
  • 1970-01-01
  • 2017-01-28
  • 2015-01-11
相关资源
最近更新 更多