0
出於某種原因,我無法使正則表達式適用於多行字符串。它工作正常,網絡上無法在Objective-C中進行正則表達式工作
我有這樣的文字:
First Line //remove leading spaces
Second Line //nothing to remove
Third Line //remove leading spaces
Fourth Line
should be 1 line //remove leading spaces and new line next char is not a capital letter
Fifth line //remove leading spaces, keep new line between the two sentences
我試圖用這個表達式
^ +|\b\n\b
它的工作原理幾乎罰款(除非它消除更多的新線路在線正則表達式編輯器,如http://rubular.com/r/u4Ao5oqeBi
但它只是從第一行刪除前導空間 這是我的代碼:
NSString *myText = @" First Line (remove leading spaces\nSecond Line (nothing to remove\n Third Line (remove leading spaces\n Fourth Line\nshould be 1 line (remove leading spaces and new line next char is not a capital letter\n\n Fifth line (remove leading spaces, keep new line between the two sentences";
NSLog(myText);
NSString *resultText = [myText stringByReplacingOccurrencesOfString: @"^ +|\\b\\n\\b"
withString: @""
options: NSRegularExpressionSearch
range: NSMakeRange(0, myText.length)];
NSLog(resultText);
我嘗試類似你所說的東西,但它沒有工作,要麼是因爲我沒有選擇使用NSRegularExpressionAnchorsMatchLines。我修改了你的解決方案,並讓它工作。謝謝! – Tibidabo 2015-03-31 11:16:03