我需要从服务器响应数据中删除一些单词。
使用我得到的正则表达式提取器
<span class="snippet_word">Działalność</span> <span class="snippet_word">lecznicza</span>.</a>
我只需要:“Działalność lecznicza”
所以我在 Beanshell 中编写了一个程序,它应该这样做,但有一个问题,因为我得到了
“莱克兹尼察莱克兹尼察”
这是我的程序:
import java.util.regex;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String pattern = "\\w+(?=\\<)";
String co = vars.get("tresc");
int len = Integer.parseInt(vars.get("length"));
String phrase="";
StringBuffer sb = new StringBuffer();
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(co);
for(i=0; i < len ;i++){
if (m.find()){
strbuf = new StringBuffer(m.group(0));
}
else {
phrase="notfound";
}
sb.append(" ");
sb.append(strbuf);
}
phrase = sb.toString();
return phrase;
tresc - 是我提取模式词的来源。 长度 - 告诉我要提取多少个单词。
对于没有国家字符的短语,程序运行良好。这就是为什么我认为编码或此处某处存在问题:
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(co);
但我不知道如何更改我的代码。
最佳答案
\w
不匹配 unicode。要在正则表达式中匹配 unicode,您可以使用 \p{L}
:
String pattern = "\\p{L}+(?=\\<)";
尽管对于此类工作,我建议使用 XML 解析器,因为正则表达式完全不适合解析 HTML/XML,如 this post 中所述。
关于java - Jmeter - beanshell 中的正则表达式 (matcher()/pattern() ) 正在切割国家字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16715437/