Java 从字符串中提取模式
我有一个随机字符串,需要从中匹配某个模式并将其解析出来 我的绳子- {“sid”:“zw9cmv1pzybexi”,“parentId”:null,“time”:1373271966311,“color”:“e94d57”,“userId”:“255863”,“st”:“comment”,“type”:“section”,“cType”:“parent”},{},null,null,null,null,{“sid”:“zwldv1lx4f7ovx”,“parentId”:“zw9cmv1pzybexi”,“time”:“color”:“1373347545798”,“color”:“userId”,“section”:“userId”,“section”<169,“cType”:“child”}、{}、null、null、null、null、{“sid”:“zw76w68c91mhbs”、“parentId”:“zw9cmv1pzybexi”、“time”:1373356224065,“color”:“#774697”,“userId”:“5216907”,“st”:“comment”,“type”:“section”,“cType”:“child”Java 从字符串中提取模式,java,string,parsing,Java,String,Parsing,我有一个随机字符串,需要从中匹配某个模式并将其解析出来 我的绳子- {“sid”:“zw9cmv1pzybexi”,“parentId”:null,“time”:1373271966311,“color”:“e94d57”,“userId”:“255863”,“st”:“comment”,“type”:“section”,“cType”:“parent”},{},null,null,null,null,{“sid”:“zwldv1lx4f7ovx”,“parentId”:“zw9cmv1pzyb
从上面我想解析出来(使用正则表达式)userId属性的所有值。有人能帮我怎么做吗?这是一个随机字符串而不是JSON。你能为我提供一个正则表达式解决方案吗?这是一个随机字符串吗?对我来说它看起来像JSON,如果是,我建议优先使用正则表达式。当遇到特定语言/语法时,正确的做法是r是使用相应的解析器,而不是(可能)脆弱的regexp。这是一个随机字符串吗?我觉得它像JSON,如果是,我建议优先使用一个,而不是一个regexp。当面对特定的语言/语法时,正确的做法是使用相应的解析器,而不是(可能)一个易碎的regexp。它是一种JSON格式,因此必须使用JSON解析器:
JSONArray array = new JSONArray(yourString);
for (int i=0;i<array.length();i++){
JSONObject jo = inputArray.getJSONObject(i);
userId = jo.getString("userId");
}
它是JSON格式,因此您必须使用JSON解析器:
JSONArray array = new JSONArray(yourString);
for (int i=0;i<array.length();i++){
JSONObject jo = inputArray.getJSONObject(i);
userId = jo.getString("userId");
}
使用JSON解析器而不是使用正则表达式,您的代码将更具可读性和可维护性
使用JSON解析器而不是使用正则表达式,您的代码将更具可读性和可维护性
要获取用户ID,可以使用以下模式:
String input = "{\"sid\":\"zw9cmv1pzybexi\",\"parentId\":null,\"time\":1373271966311,\"color\":\"#e94d57\",\"userId\":\"255863\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"parent\"},{},null,null,null,null,{\"sid\":\"zwldv1lx4f7ovx\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373347545798,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},{},null,null,null,null,null,{\"sid\":\"zw76w68c91mhbs\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373356224065,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},";
Pattern p = Pattern.compile("\"userId\":\"(.*?)\"");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group(1));
}
哪些产出:
255863
5216907
5216907
如果需要完整字符串“userId”:“xxxx”,可以使用
m.group();
而不是m.group(1);
来获取用户ID,可以使用以下模式:
String input = "{\"sid\":\"zw9cmv1pzybexi\",\"parentId\":null,\"time\":1373271966311,\"color\":\"#e94d57\",\"userId\":\"255863\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"parent\"},{},null,null,null,null,{\"sid\":\"zwldv1lx4f7ovx\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373347545798,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},{},null,null,null,null,null,{\"sid\":\"zw76w68c91mhbs\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373356224065,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},";
Pattern p = Pattern.compile("\"userId\":\"(.*?)\"");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group(1));
}
哪些产出:
255863
5216907
5216907
如果您想要完整的字符串
“userId”:“xxxx”
,您可以使用m.group();
而不是m.group(1);
,正如其他人已经告诉您的,它看起来像一个JSON字符串,但是如果您真的想自己解析这个字符串,您可以使用以下代码:
final Pattern pattern = Pattern.compile("\"userId\":\"(\\d+)\"");
final Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
匹配器将匹配每个
“userId”:“12345”
模式。matcher.group(1)
将返回每个用户id,12345
,在这种情况下(matcher.group()
不带参数返回整个组,即“userId”:“12345”
).正如其他人已经告诉您的,它看起来像一个JSON字符串,但是如果您真的想自己解析这个字符串,您可以使用以下代码:
final Pattern pattern = Pattern.compile("\"userId\":\"(\\d+)\"");
final Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
匹配器将匹配每个
“userId”:“12345”
模式。matcher.group(1)
将返回每个用户id,12345
在这种情况下(matcher.group()
不带参数返回整个组,即“userId”:“12345”
)。这是您要的正则表达式代码
//assign subject
String subject = "{\"sid\":\"zw9cmv1pzybexi\",\"parentId\":null,\"time\":1373271966311,\"color\":\"#e94d57\",\"userId\":\"255863\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"parent\"},{},null,null,null,null,{\"sid\":\"zwldv1lx4f7ovx\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373347545798,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},{},null,null,null,null,null,{\"sid\":\"zw76w68c91mhbs\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373356224065,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},";
//specify pattern and matcher
Pattern pat = Pattern.compile( "userId\":\"(\\d+)", Pattern.CASE_INSENSITIVE|Pattern.DOTALL );
Matcher mat = pat.matcher( subject );
//browse all
while ( mat.find() )
{
System.out.println( "result [" + mat.group( 1 ) + "]" );
}
当然,我建议使用JSON解析器来解决这个问题,比如
问候
克里斯托弗这是你要的正则表达式代码
//assign subject
String subject = "{\"sid\":\"zw9cmv1pzybexi\",\"parentId\":null,\"time\":1373271966311,\"color\":\"#e94d57\",\"userId\":\"255863\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"parent\"},{},null,null,null,null,{\"sid\":\"zwldv1lx4f7ovx\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373347545798,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},{},null,null,null,null,null,{\"sid\":\"zw76w68c91mhbs\",\"parentId\":\"zw9cmv1pzybexi\",\"time\":1373356224065,\"color\":\"#774697\",\"userId\":\"5216907\",\"st\":\"comment\",\"type\":\"section\",\"cType\":\"child\"},";
//specify pattern and matcher
Pattern pat = Pattern.compile( "userId\":\"(\\d+)", Pattern.CASE_INSENSITIVE|Pattern.DOTALL );
Matcher mat = pat.matcher( subject );
//browse all
while ( mat.find() )
{
System.out.println( "result [" + mat.group( 1 ) + "]" );
}
当然,我建议使用JSON解析器来解决这个问题,比如
问候
Christopher您能告诉我们您为此尝试了什么吗?请通过发布您尝试过的代码帮助我们吗?不需要在标题中添加主标记。您能告诉我们您为此尝试了什么吗?请通过发布您尝试过的代码帮助我们吗?不需要在标题中添加主标记。它是一个随机字符串,而不是JS你能为我提供一个正则表达式解决方案吗?这是一个随机字符串而不是JSON。你能为我提供正则表达式解决方案吗?这是一个随机字符串而不是JSON。你能为我提供正则表达式解决方案吗?这是一个随机字符串而不是JSON。你能为我提供正则表达式解决方案吗?谢谢!这满足了我的要求。谢谢!这是f满足我的要求。