java - How to extract the 4 digit numbers using a regex -
i want extract numbers after company_id:
part , store in variable. string looks following.
string company ="{\"company_id\":4100\"data\" \"drm_user_id\":572901936637129135\"company_id\":3070,\"data\",\"company_id\":4061,\"data\"}"
in case, regex extract numbers 4100, 3070 , 4061. have many other numbers on string, did not include them save space. numbers want ones 4 digits , come after company_id:
part. tried following
pattern pattern = pattern.compile("(\\d{4})"); matcher matcher = pattern.matcher(company);
however, returns first digit , not other two.
you can retrieve company ids using regex \"company_id\":(\\d{4})
. makes sure numbers after "company_id"
element. captures in group.
string company ="{\"company_id\":4100\"data\" \"drm_user_id\":572901936637129135\"company_id\":3070,\"data\",\"company_id\":4061,\"data\"}"; pattern pattern = pattern.compile("\"company_id\":(\\d{4})"); matcher matcher = pattern.matcher(company); while (matcher.find()) { string companyid = matcher.group(1); system.out.println(companyid); }
it seems string not valid json can't use json parser.
in code, retrieve first id because problably didn't loop on matches.
Comments
Post a Comment