我试图从一些笔记中提取出以.story结尾的单词。 这个词总是被放置在一些链接,如bla:///bla/bla/bla/.../word.story 。 笔记可能包含多个链接,这些笔记的格式可能会有所不同,但在那里我总是会以bla///../..../bla.storyformsbla///../..../bla.story 。
直到现在我已经使用了下面的expression式: [string]$story_name = Select-String \w+..story -input $notes -AllMatches | Foreach {$_.matches -replace ('\.story','')} [string]$story_name = Select-String \w+..story -input $notes -AllMatches | Foreach {$_.matches -replace ('\.story','')}但是现在我正面临着一些问题,因为看起来如果链接包含的条目是bla:///bla/blablaistory/bla/bla/word.story比这个expression式还会select那个包含'istory'的单词,我不希望发生这种情况。 我应该用什么来避免这种情况?
$notes = @" alalala/bla//blablahistory/somethingnice.istory alalala/bla//blablahistory/somethingnice.story alalala/bla//blablahistory/somethingverynice.story "@ $RE = [RegEx]'/([^/]+)\.story' $storyName = $notes -split "`n" | Select-String $RE -AllMatches | Foreach {$_.Matches.Groups[1]} $storyName -split "`n"
示例输出:
> .\SF_852359.ps1 somethingnice somethingverynice
在问题中更复杂的正则expression式如下:
[^/]是一个否定类,除了斜线之外,其他都是匹配的 [^/]+尾随加意味着至less有一个以前。 ([^/]+)圆括号标记第一个(这里只是)捕获组 /([^/]+)\.story主导斜线和尾随字面。