Я пытаюсь сопоставить шаблон в строке Java (json one). Этот шаблон соответствует строке несколько раз, но также соответствует строке, содержащей другие. Позвольте мне объяснить себе пример.Получение субматриц, когда регулярное выражение совпадает с большой строкой
String json = "IRRELEVANT_TEXT{'/element|1717_todossavoy/480/': {item_url:'/element|1717_Lorem/64/', item_description: 'Lorem ipsum dolor sit amet'},'/element|1717_Marcrie/480/': {item_url:'/element|1717_Vestibulum/64/', item_description: ' Vestibulum enim tellus, sodales sit amet consequat ut'},'/element|1717_Cannes05/434/': {item_url:'/element|1717_Nullam/64/', item_description: 'Nullam gravida risus vehicula nisi egestas'},'/element|1717_babelsavoy/266/': {item_url:'/element|1717_Pellentesque/64/', item_description: 'Pellentesque habitant morbi tristique senectus'}};IRRELEVANT";
Matcher matcher = Pattern.compile("/element.*480/").matcher(json);
while(matcher.find()) {
System.out.println(matcher.group());
}
Я получаю следующее:
/element|1717_todossavoy/480/': {item_url:'/element|1717_Lorem/64/', item_description: 'Lorem ipsum dolor sit amet'},'/element|1717_Marcrie/480/
Но я хотел бы получить 2 следующие клавиши:
/element|1717_todossavoy/480/
/element|1717_Marcrie/480/
Что я делаю неправильно?
Thats beacause из жадных регулярных выражений. Посмотрите на http://stackoverflow.com/a/2301298/891391 – yatul