Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java使用正则表达式查找和替换字符串_Java - Fatal编程技术网

Java使用正则表达式查找和替换字符串

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

我需要以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(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?