Jmeter - regex in beanshell (matcher()/pattern() ) is cutting national characters

霸气de小男生 提交于 2019-12-10 10:06:02

问题


i need to cut some words from server response data.

Use Regular Expression Extractor I get

<span class="snippet_word">Działalność</span> <span class="snippet_word">lecznicza</span>.</a>

from that i need just: "Działalność lecznicza"

so i write a program in Beanshell which should do that and there's a problem because i get

"lecznicza lecznicza"

Here is my program:

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 - is my source from I extract pattern word. Length - tells me how many words i'm extracting.

Program is working fine for phrase without national characters. Thats why I think there is some problem with encoding or somewhere here:

Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(co);

but i don't know how to change my code.


回答1:


\w does not match unicode. To match unicode in regex, you can use \p{L}:

String pattern = "\\p{L}+(?=\\<)";

Although for this type of work I would recommend using an XML parser as regular expressions are completely unsuitable for parsing HTML/XML as described in this post



来源:https://stackoverflow.com/questions/16715437/jmeter-regex-in-beanshell-matcher-pattern-is-cutting-national-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!