正则表达式从句子中删除整个字符串而不是子字符串

时间:2017-02-15 07:07:06

标签: java regex scala

我对正则表达非常新。我想在scala或java中使用正则表达式替换句子中的字符串。

实施例

  

“我是scala的新手,scalapark不同”

我想从此语句中删除“scala”字符串而不是“scalapark”。

  

“我是新手,scalapark不同”

如何使用正则表达式执行此操作。

提前致谢

1 个答案:

答案 0 :(得分:3)

你可以试试这个

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewcell") as! tableviewcell
    let theString : String = "</p><img src='https://pbs.twimg.com/profile_images/655066410087940096/QSUlrrlm.png' width=100 height=100 /><h2>Subheader</h2>"

    let theAttributedString = try! NSAttributedString(data: theString.data(using: String.Encoding.utf8, allowLossyConversion: false)!,options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],  documentAttributes: nil)

    cell.textview.attributedText =  theAttributedString

    cell.webview.loadHTMLString(theString, baseURL: nil)

    return cell
}

解释

  1. String s = "I am new to scala and scalapark is differnt"; s = s.replaceAll("\\bscala\\b", ""); 表示 boundary
  2. \\b只匹配 scala