想要从String中删除标签

时间:2022-08-27 17:04:08

I have the below string which contains some custom tags. I want to remove those custom tags from the String. Could you please help me how to remove the tags.

我有下面的字符串,其中包含一些自定义标签。我想从String中删除这些自定义标记。你能帮我解决一下如何删除标签吗?

 String temp = "[p]test to remove tags started with braces [B]bold text [/B][I]italic text [/I] [U]underlined Text[/U] bla bla [/P]"

5 个解决方案

#1


2  

You can do this easily enough with replaceAll, which accepts a regular expression.

您可以使用replaceAll轻松完成此操作,replaceAll接受正则表达式。

temp = temp.replaceAll("\\[/?\\w\\]","")

\\[ means a literal left square bracket.
/? means an optional forward slash.
\\w means a word character (such as a letter).
\\] means a literal right square bracket.

\\ [表示左侧方括号。 /?表示可选的正斜杠。 \\ w表示单词字符(例如字母)。 \\]表示文字右方括号。

This sequence in combination should match all the tags you listed in your question, and replacing them with the empty string will effectively remove them.

此序列组合应与您在问题中列出的所有标记匹配,并用空字符串替换它们将有效地删除它们。

#2


0  

You could use replace() for every set of characters that you want to remove like this :

您可以对要删除的每组字符使用replace(),如下所示:

temp = temp.replace("[p]", "");
temp = temp.replace("[B]", "");
temp = temp.replace("[/B]", "");
temp = temp.replace("[I]", "");
temp = temp.replace("[/I]", "");
temp = temp.replace("[U]", "");
temp = temp.replace("[/U]", "");
temp = temp.replace("[/P]", "");

#3


0  

String cleanedString = temp.replaceAll("\\[[a-zA-Z\\/]+\\]", "");

#4


0  

Suggestions so far have been messy and take up multiple lines because they address each [] individually. This regex, \\[(.*?)\\], handles them all at once

到目前为止,建议一直很混乱,因为它们分别对每个[]进行处理。这个正则表达式,\\ [(。*?)\\],一次处理它们

public class Test {
    public static void main(String[] args) {
        String temp = "[p]test to remove tags started with braces [B]bold text [/B][I]italic text [/I] [U]underlined Text[/U] bla bla [/P]";
        temp = temp.replaceAll("\\[(.*?)\\]","");
        System.out.println(temp);
    }
}

A brilliant website for regexes is regex101. This allows you to test your own regexes to see if they work and how the work. For the one given above, click here, works like this.

一个辉煌的正则表达网站是regex101。这允许您测试自己的正则表达式,看它们是否有效以及如何工作。对于上面给出的那个,点击这里,就像这样工作。

  • \\[ Matches the character [ literally
  • \\ [匹配角色[字面意思
  • (.*?) is a capturing group which matches any, excluding new line characters.
    • *? is a lazy quantifier which matches all the characters inbetween [] in this case
    • *?是一个惰性量词,在这种情况下匹配between []之间的所有字符
  • (。*?)是一个匹配任何的捕获组,不包括换行符。 *?是一个惰性量词,在这种情况下匹配between []之间的所有字符
  • \\] Matches the character ] literally
  • \\]按字面意思匹配字符

#5


-1  

What I would do is to make a regular expression and use the method replace_all() like so:

我要做的是创建一个正则表达式并使用方法replace_all(),如下所示:

temp.replaceAll("\\[[a-zA-Z\\/]+\\]", ""); 

This post may help you.

这篇文章可能对你有帮助。

Use this to make your own regex and test it : Regex Tester

使用它来制作自己的正则表达式并测试它:Regex Tester

#1


2  

You can do this easily enough with replaceAll, which accepts a regular expression.

您可以使用replaceAll轻松完成此操作,replaceAll接受正则表达式。

temp = temp.replaceAll("\\[/?\\w\\]","")

\\[ means a literal left square bracket.
/? means an optional forward slash.
\\w means a word character (such as a letter).
\\] means a literal right square bracket.

\\ [表示左侧方括号。 /?表示可选的正斜杠。 \\ w表示单词字符(例如字母)。 \\]表示文字右方括号。

This sequence in combination should match all the tags you listed in your question, and replacing them with the empty string will effectively remove them.

此序列组合应与您在问题中列出的所有标记匹配,并用空字符串替换它们将有效地删除它们。

#2


0  

You could use replace() for every set of characters that you want to remove like this :

您可以对要删除的每组字符使用replace(),如下所示:

temp = temp.replace("[p]", "");
temp = temp.replace("[B]", "");
temp = temp.replace("[/B]", "");
temp = temp.replace("[I]", "");
temp = temp.replace("[/I]", "");
temp = temp.replace("[U]", "");
temp = temp.replace("[/U]", "");
temp = temp.replace("[/P]", "");

#3


0  

String cleanedString = temp.replaceAll("\\[[a-zA-Z\\/]+\\]", "");

#4


0  

Suggestions so far have been messy and take up multiple lines because they address each [] individually. This regex, \\[(.*?)\\], handles them all at once

到目前为止,建议一直很混乱,因为它们分别对每个[]进行处理。这个正则表达式,\\ [(。*?)\\],一次处理它们

public class Test {
    public static void main(String[] args) {
        String temp = "[p]test to remove tags started with braces [B]bold text [/B][I]italic text [/I] [U]underlined Text[/U] bla bla [/P]";
        temp = temp.replaceAll("\\[(.*?)\\]","");
        System.out.println(temp);
    }
}

A brilliant website for regexes is regex101. This allows you to test your own regexes to see if they work and how the work. For the one given above, click here, works like this.

一个辉煌的正则表达网站是regex101。这允许您测试自己的正则表达式,看它们是否有效以及如何工作。对于上面给出的那个,点击这里,就像这样工作。

  • \\[ Matches the character [ literally
  • \\ [匹配角色[字面意思
  • (.*?) is a capturing group which matches any, excluding new line characters.
    • *? is a lazy quantifier which matches all the characters inbetween [] in this case
    • *?是一个惰性量词,在这种情况下匹配between []之间的所有字符
  • (。*?)是一个匹配任何的捕获组,不包括换行符。 *?是一个惰性量词,在这种情况下匹配between []之间的所有字符
  • \\] Matches the character ] literally
  • \\]按字面意思匹配字符

#5


-1  

What I would do is to make a regular expression and use the method replace_all() like so:

我要做的是创建一个正则表达式并使用方法replace_all(),如下所示:

temp.replaceAll("\\[[a-zA-Z\\/]+\\]", ""); 

This post may help you.

这篇文章可能对你有帮助。

Use this to make your own regex and test it : Regex Tester

使用它来制作自己的正则表达式并测试它:Regex Tester