Java使用正则表达式查找和替换字符串
我需要以Regex格式指定字符串find,以便可以找到head标记,无论其格式是Java使用正则表达式查找和替换字符串,java,Java,我需要以Regex格式指定字符串find,以便可以找到head标记,无论其格式是或或。如何以正则表达式格式指定查找字符串 String source = "<html >The quick brown fox jumps over the brown lazy dog.</html >"; String find = "<html>"; String replace = ""; Pattern pattern = Pattern.compile
或
或
。如何以正则表达式格式指定查找字符串
String source = "<html >The quick brown fox jumps over the brown lazy dog.</html >";
String find = "<html>";
String replace = "";
Pattern pattern = Pattern.compile(find);
Matcher matcher = pattern.matcher(source);
String output = matcher.replaceAll(replace);
System.out.println("Source = " + source);
System.out.println("Output = " + output);
String source=“敏捷的棕色狐狸跳过棕色懒狗。”;
字符串find=“”;
字符串替换=”;
Pattern=Pattern.compile(find);
Matcher Matcher=pattern.Matcher(源);
字符串输出=matcher.replaceAll(replace);
System.out.println(“Source=“+Source”);
System.out.println(“Output=“+Output”);
尽管您可以通过执行
来解决问题,但不应使用regex处理HTML
\\s*
表示0个或更多的空格。虽然您可以通过执行
来解决问题,但不应使用regex处理HTML
\\s*
表示0个或多个空格。要提取标记内的文本,请使用以下命令
String source = "<html >The quick brown fox jumps over the brown lazy dog.</html >";
System.out.println( source.replaceAll( "^<\\s*html\\s*>(.*)<\\s*\\/html\\s*>$", "$1" ) );
// output is:
// The quick brown fox jumps over the brown lazy dog.
String source=“敏捷的棕色狐狸跳过棕色懒狗。”;
System.out.println(source.replaceAll(“^(.*)$”,“$1”);
//输出为:
//敏捷的棕色狐狸跳过棕色懒狗。
但是尽量避免使用regexp解析html。阅读。要提取标签中的文本,请使用以下命令
String source = "<html >The quick brown fox jumps over the brown lazy dog.</html >";
System.out.println( source.replaceAll( "^<\\s*html\\s*>(.*)<\\s*\\/html\\s*>$", "$1" ) );
// output is:
// The quick brown fox jumps over the brown lazy dog.
String source=“敏捷的棕色狐狸跳过棕色懒狗。”;
System.out.println(source.replaceAll(“^(.*)$”,“$1”);
//输出为:
//敏捷的棕色狐狸跳过棕色懒狗。
但是尽量避免使用regexp解析html。阅读。不要尝试使用正则表达式解析HTML!尝试阅读有关XPath的内容。非常有用。
虽然默认情况下,
XPath
将尝试验证您的文档,但您可以尝试HtmlCleaner
使其有效。不要尝试使用正则表达式解析HTML!尝试阅读有关XPath的内容。非常有用。
虽然默认情况下,
XPath
将尝试验证您的文档,但您可以尝试HtmlCleaner
使其有效。此示例可能对您有所帮助
String source = "<html >The quick brown fox jumps over the brown lazy dog.</html >";
String find = "\\<.*?>";
String replace = "";
Pattern pattern = Pattern.compile(find);
Matcher matcher = pattern.matcher(source);
String output = matcher.replaceAll(replace);
System.out.println("Source = " + source);
System.out.println("Output = " + output);
String source=“敏捷的棕色狐狸跳过棕色懒狗。”;
字符串find=“\\”;
字符串替换=”;
Pattern=Pattern.compile(find);
Matcher Matcher=pattern.Matcher(源);
字符串输出=matcher.replaceAll(replace);
System.out.println(“Source=“+Source”);
System.out.println(“Output=“+Output”);
此示例可能对您有所帮助
String source = "<html >The quick brown fox jumps over the brown lazy dog.</html >";
String find = "\\<.*?>";
String replace = "";
Pattern pattern = Pattern.compile(find);
Matcher matcher = pattern.matcher(source);
String output = matcher.replaceAll(replace);
System.out.println("Source = " + source);
System.out.println("Output = " + output);
String source=“敏捷的棕色狐狸跳过棕色懒狗。”;
字符串find=“\\”;
字符串替换=”;
Pattern=Pattern.compile(find);
Matcher Matcher=pattern.Matcher(源);
字符串输出=matcher.replaceAll(replace);
System.out.println(“Source=“+Source”);
System.out.println(“Output=“+Output”);
是否为
有效html?是否为
有效html?