【问题标题】:Find the text between bbCode查找 bbCode 之间的文本
【发布时间】:2014-07-24 14:31:14
【问题描述】:

我正在尝试从字符串中删除 BBCode。

这是我的字符串:

String wording = "Teststring 
[URL=\"http://www.test.ch/status\"]http://www.test.ch/status[/URL] [IMG]http://sit.corproot.net/uploads/659_untitled.png[/IMG] \n\n"

这个我已经试过了:

  wording?.replaceAll("\\[URL=\".*\\](.*?)\\[/URL\\]", "")

我的目标字符串应该是:

Teststring http://www.test.ch/status \n\n"

但是当我运行代码时,它不会替换任何东西

我做错了什么?

亲切的问候

【问题讨论】:

  • 为什么是? 在措辞之后?
  • 我使用 Groovy 脚本。这是保存导航操作符。
  • 我认为 / 不需要在 RegEx 中转义,因此关闭的 BBTag 永远不会匹配。通过查看输入 [URL="asd"][\/URL] 进行验证。
  • @CollinG is \n 表示换行符或文字 \n ?
  • \n 是来自连接器的真实字符串。但是gui确实将其用作换行符。但你可以忽略它。只是一个例子

标签: java regex bbcode


【解决方案1】:
public static void main(String[] args) {

  String s = "Teststring URL=\"http://www.test.ch/status\"]http://www.test.ch/status[/URL] [IMG]http://sit.corproot.net/uploads/659_untitled.png[/IMG] \n\n";

  Pattern p = Pattern.compile("(?s)^(\\w+)[^\\[]+\\[URL=\"(.*)\"\\].*");
  Matcher m = p.matcher(s);

  if (m.matches()) {
      System.out.println(String.format("%s %s \\n\\n", m.group(1), m.group(2)));
  }

}

输出:

Teststring http://www.test.ch/status \n\n

【讨论】:

    【解决方案2】:

    正则表达式:

    \s*\[URL=\\".*\](.*?)\[\/URL\]|\s*\[IMG\].*?\[\/IMG\]\s*
    

    Java 正则表达式字符串是,

    "\\s*\\[URL=\\\\\".*\\](.*?)\\[\\/URL\\]|\\s*\\[IMG\\].*?\\[\\/IMG\\]\\s*"
    

    替换字符串:

     $1
    

    DEMO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 2014-11-05
      • 2013-04-04
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      相关资源
      最近更新 更多