2017-03-10 117 views
0

我在java中有一个实际上是JSON对象的字符串。里面的字符串我试图找到匹配这个在java中查找正则表达式

“的URI”的模式:“www.google.com”,“www.yahoo.com”]

我需要创建帮助一个模式字符串来找到匹配。我不知道自动机理论和正则表达式。

注意:上述子字符串将始终以“uris”开始并以“]”结尾,但中间可以有任意数量的空格。

+1

这有什么错调用'YourJsonObject.getJSONArray(” uris“);'和遍历数组使用循环等 –

+0

内容应该总是像你这样的url,或者它可以是任何东西? –

+1

为什么不使用适当的JSON解析器?你可以避免许多潜在的问题,比如处理只以'uris'结尾的键,或者在'key =“foo uris:[something]”'等其他键的值内搜索。 – Pshemo

回答

2

您可以使用下面的正则表达式

"uris".*?\] 

看到regex demo/explanation

的Javademo

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

class RegEx { 
    public static void main(String[] args) { 
     String s = "\"uris\" : [\"www.google.com\", \"www.yahoo.com\"]"; 
     String r = "\"uris\".*?\\]"; 
     Pattern p = Pattern.compile(r); 
     Matcher m = p.matcher(s); 
     while (m.find()) { 
      System.out.println(m.group()); //"uris" : ["www.google.com", "www.yahoo.com"] 
     } 
    } 
} 
+0

感谢您的回答,它解决了我的问题。 – SHAHS