Java 正则表达式问题

2016-05-04 10:35:30 +08:00
 ZhangTingkuo
encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB');

headers: {
	'VerificationToken': 'T1g_69b6CqBB1C0kO7XBYp_H_eVCUjnCU6GB7o3lx7VHCpSgKncivkOQuKPAxR2czgCxBb1ctnA2uK-GsIYijNH_F5Y1:Xn2ZkeeD-Ythul_Y6GxHrOcuW7bLwrUQxwf23E9hNVDPVQupoTzIdyvSch-z2-f3Ay1m8yRiISslUetKVMi3ExT1t3k1'
},

以上是一段 JavaScript 中的内容,现在要提取PublicKeyVerificationToken的值,也就是

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB

T1g_69b6CqBB1C0kO7XBYp_H_eVCUjnCU6GB7o3lx7VHCpSgKncivkOQuKPAxR2czgCxBb1ctnA2uK-GsIYijNH_F5Y1:Xn2ZkeeD-Ythul_Y6GxHrOcuW7bLwrUQxwf23E9hNVDPVQupoTzIdyvSch-z2-f3Ay1m8yRiISslUetKVMi3ExT1t3k1

正则表达式应该怎么写呢???

1742 次点击
所在节点    问与答
7 条回复
kindjeff
2016-05-04 10:43:01 +08:00
正则真不会~~这个直接用单引号分割字符串再取就可以呀
ZhangTingkuo
2016-05-04 10:46:55 +08:00
@kindjeff 这只是网页中的一段,太长了。。
Lonely
2016-05-04 11:04:02 +08:00
encrypt\.setPublicKey\('(\w+)'\)
Lonely
2016-05-04 11:17:55 +08:00
headers:\s*{\s*'VerificationToken': '([-\w:_]+)'\s*}
funsunz
2016-05-04 11:28:56 +08:00
```java
String s = "encrypt.setPublicKey('MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCp0wHYbg/NOPO3nzMD3dndwS0MccuMeXCHgVlGOoYyFwLdS24Im2e7YyhB0wrUsyYf0/nhzCzBK8ZC9eCWqd0aHbdgOQT6CuFQBMjbyGYvlVYU2ZP7kG9Ft6YV6oc9ambuO7nPZh+bvXH0zDKfi02prknrScAKC0XhadTHT3Al0QIDAQAB');\n\nheaders: {"
+"\n'VerificationToken': 'T1g_69b6CqBB1C0kO7XBYp_H_eVCUjnCU6GB7o3lx7VHCpSgKncivkOQuKPAxR2czgCxBb1ctnA2uK-GsIYijNH_F5Y1:Xn2ZkeeD-Ythul_Y6GxHrOcuW7bLwrUQxwf23E9hNVDPVQupoTzIdyvSch-z2-f3Ay1m8yRiISslUetKVMi3ExT1t3k1'"
+"\n},";
Matcher m = Pattern.compile("encrypt\\.setPublicKey\\(\\s*'([^']+)'.*\\s*.*VerificationToken'\\s*:\\s*'([^']+)'").matcher(s);
if (m.find()){
String publicKey = m.group(1);
String VerificationToken = m.group(2);
System.out.println(publicKey);
System.out.println(VerificationToken);
}
```
funsunz
2016-05-04 11:42:48 +08:00
ZhangTingkuo
2016-05-05 09:45:25 +08:00
@funsunz 多谢,解决了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/276183

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX