我試圖用某些東西替換一個新行,或者將其洗掉,但我什至不知道它是什么型別的新行字符。我試過 \n 和 \r 作為正則運算式:
var testStr =
"""
.db $82, $14, $2c, $62, $26, $10, $28, $80, $04
.db $82, $14, $2c, $62, $26, $10, $28, $80, $04
.db $82, $08, $1e, $5e, $18, $60, $1a, $80, $04
.db $82, $08, $1e, $5e, $18, $60, $1a, $86, $04
.db $83, $1a, $18, $16, $84, $14, $1a, $18, $0e, $0c
.db $16, $83, $14, $20, $1e, $1c, $28, $26, $87
.db $24, $1a, $12, $10, $62, $0e, $80, $04, $04
.db $00
"""
testStr = testStr.replacingOccurrences(of: "^\\r*", with: "!", options: .regularExpression)
testStr = testStr.replacingOccurrences(of: "^\\n*", with: "!", options: .regularExpression)
print(testStr) // does not replace new lines
uj5u.com熱心網友回復:
看起來您已經使 RegEx 變得比它需要的要復雜一些。如評論中所述,您可以洗掉開頭的錨點^和*. 多行文字創建的換行符被捕獲\n
另外,請記住,您的縮進對于多行字串文字很重要。您希望結尾"""位于文本縮進的級別。
var testStr =
"""
.db $82, $14, $2c, $62, $26, $10, $28, $80, $04
.db $82, $14, $2c, $62, $26, $10, $28, $80, $04
.db $82, $08, $1e, $5e, $18, $60, $1a, $80, $04
.db $82, $08, $1e, $5e, $18, $60, $1a, $86, $04
.db $83, $1a, $18, $16, $84, $14, $1a, $18, $0e, $0c
.db $16, $83, $14, $20, $1e, $1c, $28, $26, $87
.db $24, $1a, $12, $10, $62, $0e, $80, $04, $04
.db $00
"""
testStr = testStr.replacingOccurrences(of: "\\n", with: "!", options: .regularExpression)
print(testStr)
產量:
.db $82, $14, $2c, $62, $26, $10, $28, $80, $04!.db $82, $14, $2c, $62, $26, $10, $28, $80, $04!.db $82, $08, $1e , $5e, $18, $60, $1a, $80, $04!.db $82, $08, $1e, $5e, $18, $60, $1a, $86, $04!.db $83, $1a, $18, $16, $84 , $14, $1a, $18, $0e, $0c!.db $16, $83, $14, $20, $1e, $1c, $28, $26, $87!.db $24, $1a, $12, $10, $62, $0 e, $80, $04, $04!.db $00
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403635.html
標籤:
