-7
A
回答
3
一个简单的正则表达式:
let sentence = "This is \"table\". There is an \"apple\" on the \"table\""
let pattern = "\"[^\"]+\"" //everything between " and "
let replacement = "____"
let newSentence = sentence.replacingOccurrences(
of: pattern,
with: replacement,
options: .regularExpression
)
print(newSentence) // This is ____. There is an ____ on the ____
如果你想保持相同的字符数,那么你可以在比赛迭代:
let sentence = "This is table. There is \"an\" apple on \"the\" table."
let regularExpression = try! NSRegularExpression(pattern: "\"[^\"]+\"", options: [])
let matches = regularExpression.matches(
in: sentence,
options: [],
range: NSMakeRange(0, sentence.characters.count)
)
var newSentence = sentence
for match in matches {
let replacement = Array(repeating: "_", count: match.range.length - 2).joined()
newSentence = (newSentence as NSString).replacingCharacters(in: match.range, with: "\"" + replacement + "\"")
}
print(newSentence) // This is table. There is "__" apple on "___" table.
相关问题
- 1. PHP快速替换字符之间字符串的内容
- 2. 替换字符串之间的字符
- 3. 替换2个特定字符之间的字符串字符
- 4. 替换两个字符串之间的字符串iphone sdk
- 5. 在Java中的2个字符串之间替换字符串
- 6. python用字符串之间的特殊字符替换空格
- 7. 如何替换Sublime中两个字符之间的字符串
- 8. 用通配符替换快速字符串
- 9. 替换部分字符串在特殊字符之间
- 10. 如何两个字符串之间替换字符
- 11. 用子串替换后续分隔符之间的字符串
- 12. 替换字符串中特定字符之间的特定字符
- 13. 替换字符串内的字符串
- 14. 字符串中的字符串替换
- 15. 快速字符串替换mod_rewrite的问题
- 16. 替换字符串之前的字符串?
- 17. 查找字符串快速
- 18. 2字符串之间的字符串
- 19. 将字符串替换为字符串
- 20. 在VBA中替换字符串中字符的最快方法
- 21. 字符串中最快的字符替换
- 22. 在字节之间发送字符串之间的字符串
- 23. PhP查找(并替换)两个不同字符串之间的字符串
- 24. JavaScript ReGeX:替换两个字符串之间的匹配字符串
- 25. 从字符串替换字符串的部分字符串
- 26. 替换字符串
- 27. 替换字符串
- 28. 替换字符串
- 29. 字符串替换
- 30. 替换字符串