<p><img src="http://www.xinhushang.com.cn/images/u/editorupload/201803/20180323103138721.png" border="0" alt="<alert>/你好</input>"><br>
</p> 类似这样的字符串,我要截取alt=“”“” 里面的值 过滤是否有特殊字符,求大佬出招,挺急的!
/**
* 从一段文本中截取你你想要的字符串
*/
@Test
public void test(){
String str = " asdferfgsergstrfghdrthehd alt=\"<alert>/你好</input>\"><br></p>asdwdaderf4fsdf";
String reg="alt=\"(.*?)\"";
Pattern pat = Pattern.compile(reg);
Matcher mat = pat.matcher(str);
String result = "";
if(mat.find()){
result = mat.group(1);
}
System.out.println("原始字符串是: "+ str);
System.out.println("截取出来的字符串是: "+ result);
// 需要怎么过滤, 过滤掉什么特殊字符 就看你自己想过滤掉什么了,过滤方法在下面
}
@Test
public void test1(){
//假如 @ 和 + 是你认为的特殊字符
String str = "<alert>/你好</input>" + "+" +"@";
System.out.println("是否含有特殊字符 " + strfliter(str));
}
/**
* 过滤一个字符串看看是否有特殊字符
*/
public boolean strfliter(String str){
//此处编写含有特殊字符的正则表达式,你想测试的 特殊字符请写在[]内, 有些特殊的要使用\转译, 我们已两个 特殊字符 @ 和 + 作为示例, 其中+需要转译, @不用转译
String reg=".*[@\\+].*";
return str.matches(reg);
}
String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(str);
return mat.replaceAll("").trim();
String regEx="[`~!@#$%^&*()+=|{}':;',\[\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(str);
return mat.replaceAll("").trim();
String regEx="[`~!@#$%^&*()+=|{}':;',\[\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(str);
return mat.replaceAll("").trim();
package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
/**
@author Administrator
*
*/
public class MainTest {
/**
@Test
public void test1(){
//假如 @ 和 + 是你认为的特殊字符
String str = "/你好" + "+" +"@";
System.out.println("是否含有特殊字符 " + strfliter(str));
}
/**
}
```package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
/**
@author Administrator
*
*/
public class MainTest {
/**
@Test
public void test1(){
//假如 @ 和 + 是你认为的特殊字符
String str = "/你好" + "+" +"@";
System.out.println("是否含有特殊字符 " + strfliter(str));
}
/**
}
String regEx="[`~!@#$%^&*()+=|{}':;',[].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";
Pattern pat = Pattern.compile(regEx);
Matcher mat = pat.matcher(str);
return mat.replaceAll("").trim();