regex - 需要正则表达式来匹配掩码中找到的确切空格数

标签 regex vb.net

所以我正在编写一个程序来根据找到的文件打印音乐书籍。我已经可以选择读取已完成的标签,但解析文件名的速度要快得多,所以我决定将其作为一个选项。用户将提供类似于以下内容的掩码:

    (%year%) %album%\%track%. %artist% - %title%

所以我需要用代码创建正则表达式。我已经快完成了,但在空间方面有问题。我需要能够匹配掩码中提供的确切空格数。这是我到目前为止所拥有的:
    ^(?<track>[^.]+)\.[ ](?![ ])(?<artist>.+?)[ ](?![ ])-[ ](?![ ])(?<title>[^.]+)\.mp3$

除了破折号之前的那个,前瞻工作一切正常。不知道为什么。它将允许多个空格(但不是 0 个空格)。所以我需要的是我可以“插入”到我找到的每个空间的掩码中的东西,它只会匹配那个空间。

最佳答案

问题

问题在于您的正则表达式命名捕获,这些捕获正在收集所有非 .人物。 [^.]字符类也匹配一个空格。所以像 \s{1}([^.]+)\s{2}\. 这样的表达式会允许:

 SomeTitle    .mp3
^^^^^^^^^^^^  
   SomeTitle  .mp3
  ^^^^^^^^^^  

捕获组将获得带下划线的部分,包括带下划线的前导或尾随空格。这基本上允许在源字符串中存在更多的空格,然后是所需的。要解决此问题,您需要提前查看 (?!\s)在每个所需的空格匹配之后,以确保字符类捕获的下一个字符不会是像 `\s{1}(?!\s)([^.]+)\s{2}(?!\s)。

解决方案

此正则表达式将从以下格式的字符串中捕获轨道、艺术家和标题:(%year%) %album%\%track%. %artist% - %title% .要确保下一个字符不是空格,请使用 (?!\s) .这是插入一个结尾
^\((?<year>[^)]*)\)\s{1}(?!\s)(?<album>[^\\]*)\\(?<track>[^.]*)\.\s{1}(?!\s)(?<artist>(?:(?!\s{1}-\s{1}).)+?[^\s])\s{1}-\s{1}(?!\s)(?<title>[^.]*?[^.\s])\.(?<ZeroLengthSpaceAfterDot>(?!\s))mp3
enter image description here

在图像中,第 1 组 = 年份,第 2 组 = 专辑,第 3 组 = 轨道,第 4 组 = 艺术家,第 5 组 = 标题,第 6 组是零空格的示例

VB.NET 代码示例:

输入文本
(%year%) %album%\%track%. %artist% - %title%.mp3
(1971) Punk Kittens\1of3. Kittens - I Like cats.mp3
(1969) Muppet Show\2of3. Pigs - Pigs In Space. mp3
(1991) Foo Shivle\3of3. Snoop Dog - Just another brick in the pound.mp3
(2009) Space Race\3of3. Sir Space Alot -  Too many Spaces.mp3

代码示例
Imports System.Text.RegularExpressions
Module Module1
  Sub Main()
    Dim sourcestring as String = "replace with your source string"
    Dim re As Regex = New Regex("^\((?<year>[^)]*)\)\s{1}(?!\s)(?<album>[^\\]*)\\(?<track>[^.]*)\.\s{1}(?!\s)(?<artist>(?:(?!\s{1}-\s{1}).)+?[^\s])\s{1}-\s{1}(?!\s)(?<title>[^.]*?[^.\s])\.(?<ZeroLengthSpaceAfterDot>(?!\s))mp3",RegexOptions.IgnoreCase OR RegexOptions.Multiline OR RegexOptions.Singleline)
    Dim mc as MatchCollection = re.Matches(sourcestring)
    Dim mIdx as Integer = 0
    For each m as Match in mc
      For groupIdx As Integer = 0 To m.Groups.Count - 1
        Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames(groupIdx), m.Groups(groupIdx).Value)
      Next
      mIdx=mIdx+1
    Next
  End Sub
End Module

火柴
$matches Array:
(
[0] => Array
    (
        [0] => (%year%) %album%\%track%. %artist% - %title%.mp3
        [1] => (1971) Punk Kittens\1of3. Kittens - I Like cats.mp3
        [2] => (1991) Foo Shivle\3of3. Snoop Dog - Just another brick in the pound.mp3
    )

[year] => Array
    (
        [0] => %year%
        [1] => 1971
        [2] => 1991
    )


[album] => Array
    (
        [0] => %album%
        [1] => Punk Kittens
        [2] => Foo Shivle
    )


[track] => Array
    (
        [0] => %track%
        [1] => 1of3
        [2] => 3of3
    )


[artist] => Array
    (
        [0] => %artist%
        [1] => Kittens
        [2] => Snoop Dog
    )

[title] => Array
    (
        [0] => %title%
        [1] => I Like cats
        [2] => Just another brick in the pound
    )


[ZeroLengthSpaceAfterDot] => Array
    (
        [0] => 
        [1] => 
        [2] => 
    )


)

空间选择
  • \s\s{1}匹配一个空格
  • \s(?!\s)匹配一个空格并确保空格后的字符不是空格。
  • \s*匹配零个或多个空格
  • \s{2,}匹配两个或多个空格
  • (?!\s)确保下一个字符不是空格
  • (?!\s{6})确保接下来的六个字符不都是空格
  • 关于regex - 需要正则表达式来匹配掩码中找到的确切空格数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17135264/

    相关文章:

    regex - REGEX Google Analytics(分析)目标

    vb.net - Select Case with "Is"operator

    asp.net - 在 VB.NET 中随机化数字而不使用范围,即最小值和最大值

    sql-server - 在 VB.NET 中获取 SQL Server 表中的行数

    vb.net - 没有时间字段的linq groupby vb.net

    .net - 系统.Data.SQLite.SQLiteException : Database is locked?

    java - 如何使用正则表达式从字符串中排除子字符串的出现?

    regex - Azure 静态网站的 URL 应不带尾随/

    正则表达式从 bash 脚本中的字符串中提取第一个 float

    Javascript 正则表达式 : remove space(s) if not surrounded by a letter