1

Добрый день, есть строка:

<div id= "10"    class  =   " .mcp   " align="100px" i11d= "10"    c11lass  =   " .mcp   " alig1n="100px">

Пробую с помощью маски [a-zA-Z]+\\s*=\\s*\".+\" вытащить все атрибуты. Использую такой код:

check("[a-zA-Z]+\\s*=\\s*\".+\"", testStr3);
static  void check(String reg, String testStr)
{
    Pattern p = Pattern.compile(reg);
    Matcher m = p.matcher(testStr);
    if(m.matches())
        out("Group " + m.group()); // out <=> System.out.println();
    else 
        out("No matches with group");
    out("While find");
    while(m.find()) {
        System.out.println(testStr.substring(m.start(), m.end()));
    }
    out("Done");
} 

При этом m.matches() всегда false. Соответственно m.group()никогда не выполняется, а while печатает просто строку со всеми атрибутами. Вопрос вот какой, как получить все атрибуты по отдельности, а не в одной строке?

2
  • 1
    Попробуйте так: /([a-zA-Z]+)\s*=\s*\"(.+?)\"/g
    – Tunker
    19 июл 2016 в 14:50
  • 1
    ^ И вероятно хвост - \"([^\"]+?)\"/g
    – splash58
    19 июл 2016 в 14:56

1 ответ 1

4

Попробуйте так:

    check("[a-zA-Z]+\\s*=\\s*\".+\"", testStr3);
    static  void check(String reg, String testStr)
    {
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(testStr);
        while (m.find()) {
            System.out.println("Group " + m.group());
            System.out.println(testStr.substring(m.start(), m.end()));
        }
        System.out.println("Done");
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.