Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
java / лекции / Лекция 2.ppt
Скачиваний:
103
Добавлен:
17.04.2018
Размер:
207.36 Кб
Скачать

Использование групп и классов с квантификаторами

Enter your regex: (dog){3}

 

 

 

Enter input

string to search: dogdogdogdogdogdog

 

I found the

text "dogdogdog" starting at

index 0 and ending at

 

index 9.

 

 

 

 

I found the

text "dogdogdog" starting at

index 9 and ending at

 

index 18.

 

 

 

 

Enter your regex: dog{3}

 

 

 

Enter input

string to search: dogdogdogdogdogdog

 

No match found.

 

 

 

Enter your regex: [abc]{3}

 

 

 

Enter input

string to search: abccabaaaccbbbc

 

I found the

text "abc" starting at index

0

and ending at index

3.

I found the

text "cab" starting at index

3

and ending at index

6.

I found the

text "aaa" starting at index

6

and ending at index

9.

I found the

text "ccb" starting at index

9

and ending at index

12.

I found the

text "bbc" starting at index

12 and ending at index

 

15.

 

 

 

 

Жадные, ленивые и захватывающие квантификаторы

Жадные (greedy) – находит соответствие шаблону наиболее длинную часть строки (по- умолчанию).

Ленивые (reluctant) – находит соответствие шаблону наиболее короткую часть строки

Захватывающий (possessive) – находит первое соответствие шаблону.

X? – жадный

X?? – ленивый

X?+ - захватывающий

Жадные, ленивые и захватывающие квантификаторы

Enter your regex: .*foo

Enter input string to search: xfooxxxxxxfoo

I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13.

Enter your regex: .*?foo

Enter input string to search: xfooxxxxxxfoo

I found the text "xfoo" starting at index 0 and ending at index 4.

I found the text "xxxxxxfoo" starting at index 4 and ending at index 13.

Enter your regex: .*+foo

Enter input string to search: xfooxxxxxxfoo No match found.

Группы

Группы (capturing groups) позволяют трактовать последовательность символов как единое целое

Нумерование групп: ((A)(B(C)))

1.((A)(B(C)))

2.(A)

3.(B(C))

4.(C)

Обратные ссылки

Совпавшие с шаблоном группы сохраняются в памяти как обратные ссылки (backreferences). Их можно использовать в шаблоне.

Enter your regex: (\d\d)\1

Enter input string to search: 1212

I found the text "1212" starting at index 0 and ending at index 4.

Enter your regex: (\d\d)\1

Enter input string to search: 1234

No match found.

 

Граничные мета-символы

 

(boundary matchers)

^

Начало строки

$

Конец строки

\b

Граница слова

\B

Не граница слова

Граничные мета-символы (boundary matchers)

Enter your regex: ^dog$

Enter input string to search: dog

I found the text "dog" starting at index 0 and ending at index 3.

Enter your regex: ^dog$

 

Enter input string to search:

dog

No match found.

 

Enter your regex: \s*dog$

 

Enter input string to search:

dog

I found the text "

dog" starting at index 0 and

ending at index 15.

 

Enter your regex: ^dog\w*

Enter input string to search: dogblahblah

I found the text "dogblahblah" starting at index 0 and ending at index 11.

Граничные мета-символы (boundary matchers)

Enter your regex: \bdog\b

 

Enter input

string to search: The dog plays in

the yard.

I found the

text "dog" starting at index 4 and

ending at

index 7.

 

 

Enter your regex: \bdog\b

 

Enter input

string to search: The doggie plays

in the yard.

No match found.

 

Enter your regex: \bdog\B

 

Enter input

string to search: The dog plays in

the yard.

No match found.

 

Enter your regex: \bdog\B

 

Enter input

string to search: The doggie plays

in the yard.

I found the

text "dog" starting at index 4 and

ending at

index 7.

 

 

Перечисление

Для определения перечислений используется вертикальная черта |.

Для включения перечисления в выражение используются группы.

Пример:

Enter your regex: dog|cat

Enter input string to search: dog,cat

I found the text "dog" starting at index 0 and ending at index 3. I found the text "cat" starting at index 4 and ending at index 7.

Enter your regex: \d+ (dog|cat)s?

Enter input string to search: 1 dog and 2 cats

I found the text "1 dog" starting at index 0 and ending at index 5.

I found the text "2 cats" starting at index 10 and ending at index 16.

Методы класса Pattern

static Pattern compile(String s) – компилирует шаблон.

String[] split(String s) – разбивает строку используя шаблон как разграничитель

Соседние файлы в папке лекции