正则表达式-Java实现-^开始位置、$结束位置、(?m)

时间:2025-02-15 11:40:30
import ;
import ;

/*
	字符串的边界匹配用的方法就是查找字符串是不是按照规定的模式开始和结束
	^ 匹配字符串的开头位置,^\s* 表示字符串以零个或多个空白开头
	$ 匹配字符串的结束位置
*/

public class StrBoundaryMatch{
	public static void main(String[] args){
		String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + 
			" <wsdl:definitions targetNamespace=\"\">" + 
			"xmlns:impl=\"\" xmlns=\"\""+
			"xmlns:apachesoap=\"/xml-soap\"";
		//匹配 <?xml> 标签的正确性
		// <\?xml.*\?>
		Pattern p = ("<\\?xml.*\\?>");
		Matcher m = (str); //<\?xml.*\?>
		(());
		while (()){
			(());
		}
		//<?xml version="1.0" encoding="UTF-8" ?>
		();

		String str2 = "This is bad, real bad!" + 
			"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + 
			" <wsdl:definitions targetNamespace=\"\">" + 
			"xmlns:impl=\"\" xmlns=\"\""+
			"xmlns:apachesoap=\"/xml-soap\"";
		//这段作为 .xml 文件内容时候是有问题的,"This is bad, real bad!" 应该作为注释
		//此时进行模式匹配的时候,希望做到的是,xml 文件以 <?xml ?> 开头,其他开头的都是错误的
		// ^\s*<\?xml.*\?>
		p = ("^\\s*<\\?xml.*\\?>");
		m = (str2);
		(()); //false
		//此时对 str 进行模式匹配
		m = (str);
		while (()){ //true
			(());
		}
		//<?xml version="1.0" encoding="UTF-8" ?>
		//已经匹配到了合法的内容
		();

		//一个 xml 文件的结尾一定是  </html>
		// </[Hh][tT][mM][lL]>\s*$
		String str3 = "</Html>   ";
		p = ("</[Hh][tT][mM][lL]>\\s*$");
		m = (str3);
		while (()){
			(());
		}
		//</Html>
	}
}
import ;
import ;
import ;
import ;
import ;
import ;

/*
	(?m) 分行匹配模式,必须放在整个模式的最前面,分行匹配模式使得正则表达式引擎吧分行符当作一个字符串分隔符
		对待。此时在分行匹配模式下 ^ 不仅仅匹配正常的字符串开头,还将匹配分隔符(换行符)后面的开始位置,类
		似的 $ 也不仅仅匹配正常字符串结尾,还将匹配分隔符(换行符)后面的结束位置
*/

public class EachLineMatch{
	public static void main(String[] args){
		StringBuffer strbuf = new StringBuffer("");
		try{
			BufferedReader read = 
				new BufferedReader(new FileReader("E:/java/正则表达式练习/RegularExpression/"));
			String str = "";
			while ((str = ()) != null){
				(str + "\r\n");
			}
		}
		catch (IOException ex){
			();
		}
		(strbuf);
		//匹配出所有的注释
		// ^\s*//.*$  也就是以 // 开头,任意字符结尾
		Pattern p = ("(?m)^\\s*//.*$");
		Matcher m = (strbuf);
		(()); // (?m)^\s*//.*$
		while (()){
			(());
		}
		/*
		//Make sure not empty
        //Init
        //Done
		*/
	}
}

<script> 
function doSpellcheck(from, field){ 
	//Make sure not empty
	if (==''){
		return false;
	} 
	//Init 
	var windowName='spelleWindow'; 
	var spellCheckURL='?formname=comment&fieldname='+; 
	...
	//Done 
	return false; 
}
</script>