给定一个包含一些 HTML 标签的字符串str ,任务是删除给定字符串str 中存在的所有标签。
例子:
做法:思路是用正则表达式来解决这个问题。可以按照以下步骤计算结果字符串:
- 获取字符串。
- 由于每个 HTML 标签都包含在尖括号 ( <> ) 中。因此,在正则表达式中使用replaceAll()函数将每个以“<”开头并以“>”结尾的子字符串替换为空字符串。
- 该函数用作:
String str;
str.replaceAll("\\", "");
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#include
using namespace std;
// Function to remove the HTML tags
// from the given string
void RemoveHTMLTags(string s)
{
const regex pattern("\\<.*?\\>");
// Use regex_replace function in regex
// to erase every tags enclosed in <>
s = regex_replace(s, pattern, "");
// Print string after removing tags
cout << s;
return ;
}
// Driver Code
int main()
{
// Given String
string str = "Geeks for Geeks";
// Function call to print the
// HTML string after removing tags
RemoveHTMLTags(str) ;
return 0;
}
// This code is contributed by yuvraj_chandra
Java
// Java program for the above approach
class GFG {
// Function to remove the HTML tags
// from the given tags
static void RemoveHTMLTags(String str)
{
// Use replaceAll function in regex
// to erase every tags enclosed in <>
str = str.replaceAll("\\<.*?\\>", "");
// Print string after removing tags
System.out.println(str);
}
// Driver Code
public static void main(String[] args)
{
String str;
// Given String
str = "Geeks for Geeks";
// Function call to print the
// HTML string after removing tags
RemoveHTMLTags(str);
}
}
Python3
# Python3 program for the
# above approach
import re
# Function to remove the HTML tags
# from the given tags
def RemoveHTMLTags(strr):
# Print string after removing tags
print(re.compile(r'<[^>]+>').sub('', strr))
# Driver code
if __name__=='__main__':
# Given String
strr = "Geeks for Geeks"
# Function call to print the HTML
# string after removing tags
RemoveHTMLTags(strr);
# This code is contributed by vikas_g
C#
// C# program for the above approach
using System;
class GFG{
// Function to remove the HTML tags
// from the given tags
static void RemoveHTMLTags(String str)
{
// Use replaceAll function in regex
// to erase every tags enclosed in <>
// str = Regex.Replace(str, "<.*?>", String.Empty)
System.Text.RegularExpressions.Regex rx =
new System.Text.RegularExpressions.Regex("<[^>]*>");
str = rx.Replace(str, "");
// Print string after removing tags
Console.WriteLine(str);
}
// Driver code
public static void Main(String []args)
{
String str;
// Given String
str = "Geeks for Geeks";
// Function call to print the
// HTML string after removing tags
RemoveHTMLTags(str);
}
}
// This code is contributed by vikas_g
输出:
Geeks for Geeks
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live