【问题标题】:Regex to match text between two delimeters?正则表达式匹配两个分隔符之间的文本?
【发布时间】:2017-05-29 15:11:22
【问题描述】:

以下是我需要在存储为文本的请求中匹配的内容示例:

[{"id":"896","name":"TinyAuras","author_id":"654","author":"Kurisu","githubFolder":"https://github.com/xKurisu/TinyAuras/blob/master/TinyAuras.csproj","count":9,"countByChampion":{"":9,"total":9 },"description":"(Beta) Aura/Buff/Debuff Tracker","udate":"1451971516","createdDays":375,"image":"https://cdn.joduska.me/forum/uploads /assemblydb/image-default.jpg","strudate":"2016-07-22 19:40","champions":null,"forum_link":"165574","assembly_compiles":true,"voted":false ,"voted_champions":[]},

我想选择该链接直到此处停止(基本上是 github 文件夹,而不是实际的 csproj)。

我有一个包含数千个链接的文件,我正在尝试提取所有这些链接并将它们放入一个文本文件中。

这是我目前对 perl 正则表达式的了解: (?<=githubFolder":").*(?=\/.+\.csproj") 但最终在第一场比赛后选择的数量超出了我的需要。有什么建议吗?

问题是,我想要 this.csproj 之前的所有内容。

所以在我的示例中,我想提取: https://github.com/xKurisu/TinyAuras/blob/master/

【问题讨论】:

  • 请分享更多示例链接,以便识别模式。
  • 我添加了工作正则表达式模式来抓取 url,只需要弄清楚如何只选择 this.csproj
  • sed 's/\(^.*\)[.]csproj["]$/\1/' file > newfile 怎么样? (如果末尾没有双引号,则可以删除["])您可以在\(之前添加"githubFolder":",如果需要删除^ "githubFolder":"
  • 抓太多了,我会用更大的例子更新帖子。
  • 哦...现在我明白了。这不适用于更新...

标签: regex linux


【解决方案1】:

这个正则表达式:

"githubFolder":"([^"]*/)[^"/]*"

选择:

https://github.com/xKurisu/TinyAuras/blob/master/

在你的例子中。

但是,正如 Jim D. 的回答所建议的那样,使用实际的 json 解析器可能会更好,这样您就不必担心间距和特殊字符。

【讨论】:

  • 您可以将捕获组内的尾部斜杠移动到捕获组外,如下所示:"githubFolder":"([^"]*)/[^"/]*"
  • 当我在正则表达式之类的东西上发短信时,你的答案有效,但是当我用 grep -o -P 测试它时,这是我的输出:cat ChampionswithGit.txt | grep -o -P 'githubFolder":"([^"]*/)[^"/]*' 输出:githubFolder":"https:\/\/github.com\/ikkeflikkeri\/LeagueSharp\/blob\ /master\/EasyCorki\/EasyCorki\/EasyCorki.csproj 有什么想法吗?
  • 由于我们正在解析一个 JSON 对象数组,人们会认为该字符串是 JSON 编码的,因此可能包含转义引号和其他需要翻译的转义序列。 JSON 还允许在标记之间插入空格。
  • @Ben 我想你已经解决了这个问题,但请参阅stackoverflow.com/questions/1891797/…
  • @Jim D. 是的,按照您的回答建议使用实际的 json 解析器可能会更好:)
【解决方案2】:

这是正则表达式:

("githubFolder":".*)\/(.*\.csproj)

1. "githubFolder":"https://github.com/removed/removed/blob/master/stophere/this.csproj      
    1.1. Group: "githubFolder":"https://github.com/removed/removed/blob/master/stophere
    1.2. Group: this.csproj

你可以在这里测试它:http://www.regexe.com

【讨论】:

  • 不要使用点匹配,如果字符串后面有另一个csproj会出错...
【解决方案3】:

此模式:(http|https):\/\/github\.com\/[\w\/]+\/ 选择您的示例中以 github.com 开头的所有目录。

【讨论】:

  • 这在 github 的命名约定中假设了太多。我更新了 OP
【解决方案4】:

试试这个正则表达式:

githubFolder":"([a-zA-Z:\/.]+\/)

它将链接分组到最后一个斜杠。

【讨论】:

    【解决方案5】:

    虽然接受的答案可能会在这里完成工作,但我只想指出,旧的 linux 工具不容易使用 JSON 来获得 100% 准确的结果,因此,它会是使用实际 JSON 解析器提取内容的最佳实践。

    一个简单的原因是字符串是 JSON 编码的,因此您需要以某种方式对其进行解码以确保获得正确的结果。另一个是 JSON 不是常规语言,它是上下文无关的。通常,您将需要比正则表达式更强大的东西。

    我熟悉的是jq,JSON 对象的数组可以按照 OP 的要求进行解析,如下所示:

    $ jq -r ' .[] | .githubFolder ' foo
    https://github.com/xKurisu/TinyAuras/blob/master/TinyAuras.csproj
    https://github.com/xKurisu/"GiantAuras"/blob/master/GiantAuras.csproj
    $
    

    文件foo在哪里

    [
      {
        "id": "896",
        "name": "TinyAuras",
        "author_id": "654",
        "author": "Kurisu</span></strong></span></a>",
        "githubFolder": "https://github.com/xKurisu/TinyAuras/blob/master/TinyAuras.csproj",
        "count": 9,
        "countByChampion": {
          "": 9,
          "total": 9
        },
        "description": "(Beta) Aura/Buff/Debuff Tracker",
        "udate": "1451971516",
        "createdDays": 375,
        "image": "https://cdn.joduska.me/forum/uploads/assemblydb/image-default.jpg",
        "strudate": "2016-07-22 19:40",
        "champions": null,
        "forum_link": "165574",
        "assembly_compiles": true,
        "voted": false,
        "voted_champions": []
      },
      {
        "id": "888",
        "name": "\"GiantAuras\"",
        "author_id": "666",
        "author": "Astaire</span></strong></span></a>",
        "githubFolder": "https://github.com/xKurisu/\"GiantAuras\"/blob/master/GiantAuras.csproj",
        "count": 90,
        "countByChampion": {
          "": 777,
          "total": 42
        },
        "description": "(Stable) Aura/Buff/Debuff Tracker",
        "udate": "1451971517",
        "createdDays": 399,
        "image": "https://cdn.joduska.me/forum/uploads/assemblydb/image-default.jpg",
        "strudate": "2016-07-22 19:40",
        "champions": null,
        "forum_link": "165574",
        "assembly_compiles": true,
        "voted": false,
        "voted_champions": []
      }
    ]
    

    【讨论】:

    • 这很有用。我最终这样做了:cat ChampionswithGit.txt | grep -oP '"githubFolder":"([^"]*/)[^"/]*' | grep -oP '.*(?=\/.+\.csproj)' | grep -oP '(?
    • @Ben 我认为\/ 来自这样一个事实,即固线(/)可以选择在 JSON 字符串中转义为\/,这或多或少是我正在尝试的指出。这可能会解决你的问题,但是有一天会有一个转义的反斜杠,你必须修复它,然后有一天,一个 unicode 转义序列会出现......
    猜你喜欢
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 2017-02-16
    • 2013-08-05
    相关资源
    最近更新 更多