Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > Про java.util.regex.Pattern объясните


Автор: PovAnd 25.3.2008, 18:18
Объясните что за фигня. Вот класс.

Код

public class Test {
    private static final String STRING = "Привет пока Hello hell"; 
    public static void main(String[] args){
        System.out.println(STRING);
        System.out.println();
        test1("(?s)[пh]");
        test1("(?i)[пh]");
        System.out.println();
        test2("(?s)[пh]");
        test2("(?i)[пh]");
        System.out.println();
        test3("(?s)[пh]");
        test3("(?i)[пh]");
        System.out.println();
        test4("(?s)[пh]");
        test4("(?i)[пh]");

    }
    
    public static void test1(String regexp){
        Matcher matcher = Pattern.compile(regexp,Pattern.UNICODE_CASE )
                .matcher(STRING);
        System.out.println(matcher.replaceAll("#"));
    }
    
    public static void test2(String regexp){
        Matcher matcher = Pattern.compile(regexp)
                .matcher(STRING);
        System.out.println(matcher.replaceAll("#"));
    }
    
    public static void test3(String regexp){
        Matcher matcher = Pattern.compile(regexp,Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE )
                .matcher(STRING);
        System.out.println(matcher.replaceAll("#"));
    }
    
    public static void test4(String regexp){
        Matcher matcher = Pattern.compile(regexp,Pattern.CASE_INSENSITIVE )
                .matcher(STRING);
        System.out.println(matcher.replaceAll("#"));
    }
}


вот отрабокта класса:

Привет пока Hello hell

#ривет #ока Hello #ell
#ривет #ока #ello #ell

Привет #ока Hello #ell
Привет #ока #ello #ell

#ривет #ока #ello #ell
#ривет #ока #ello #ell

Привет #ока #ello #ell
Привет #ока #ello #ell


Каким раком надо использовать Pattern, чтобы в итоге получилась такая строка:

Привет #ока Hello #ell

?

Автор: Kangaroo 25.3.2008, 18:37
Цитата(PovAnd @  25.3.2008,  17:18 Найти цитируемый пост)
Каким раком надо использовать Pattern, чтобы в итоге получилась такая строка:

Привет #ока Hello #ell

Сделай финт ушами  - убери все флаги, они там не нужны  smile 

Код

public static void test1(String regexp){
        Matcher matcher = Pattern.compile(regexp)
                .matcher(STRING);
        System.out.println(matcher.replaceAll("#"));
    }


Добавлено через 29 секунд
И внимательней читай ДжаваДоки, там все написано.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)