需要解析这个字符串
#Login&oauth_token=theOAUTHtoken&oauth_verifier=12345
我只需要在哪里获取 oauth_token
和oauth_verifier
键+值,使用正则表达式执行此操作的最简单方法是什么?
最佳答案
这样就可以了,你没有指定你想要的数据输出方式,所以我用逗号分隔它们。
import java.util.regex.*;
class rTest {
public static void main (String[] args) {
String in = "#Login&oauth_token=theOAUTHtoken&oauth_verifier=12345";
Pattern p = Pattern.compile("(?:&([^=]*)=([^&]*))");
Matcher m = p.matcher(in);
while (m.find()) {
System.out.println(m.group(1) + ", " + m.group(2));
}
}
}
正则表达式:
(?: group, but do not capture:
& match '&'
( group and capture to \1:
[^=]* any character except: '=' (0 or more times)
) end of \1
= match '='
( group and capture to \2:
[^&]* any character except: '&' (0 or more times)
) end of \2
) end of grouping
输出:
oauth_token, theOAUTHtoken
oauth_verifier, 12345
关于java - 使用正则表达式解析 URL 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301721/