我必須找到以“.”開頭的片段。或字串中的“:”。
如果不是在開頭的代碼段必須以空格開頭。
.snippet1 Lorem ipsum :snippet2 dolor 坐 amet .snippet3
我嘗試了以下正則運算式:
[\A\h][\.:][\w_-]
// [\A\h] At the Beginning or horizontal Whitespace
// [\.:] . or :
// [\w_-] characters
模式:未檢測到
.snippet lore
我的 Swift 測驗
let snippetRegExString = #"[\A\h][\.:][\w_-] "#
XCTAssertEqual("Test gm Test".getMatch(regExpString: snippetRegExString), "")
XCTAssertEqual("Test.gm Test".getMatch(regExpString: snippetRegExString), "")
XCTAssertEqual("Test .gm Test".getMatch(regExpString: snippetRegExString), " .gm")
XCTAssertEqual("Test.gm".getMatch(regExpString: snippetRegExString), "")
XCTAssertEqual("Test.gm test".getMatch(regExpString: snippetRegExString), "")
XCTAssertEqual("Test.gm ".getMatch(regExpString: snippetRegExString), "")
XCTAssertEqual("Test .gm".getMatch(regExpString: snippetRegExString), " .gm")
XCTAssertEqual(".gm Test".getMatch(regExpString: snippetRegExString), "")
XCTAssertEqual(" .gm Test".getMatch(regExpString: snippetRegExString), " .gm")
XCTAssertEqual(" .gm".getMatch(regExpString: snippetRegExString), " .gm")
// Test Fails
XCTAssertEqual(".gm test".getMatch(regExpString: snippetRegExString), ".gm ")
RegEx 有什么問題?
uj5u.com熱心網友回復:
以下模式正在作業:
(?<!\S)[:.][\w -]
以下是對該模式的解釋:
(?<!\S) assert that what precedes is whitespace or the start of the input
[:.] match : or .
[\w -] match a snippet word
而這里是一個正則運算式的演示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/398892.html
下一篇:R在預定義位置拆分字串
