- •Лекция 2
- •Общие сведения
- •Классы для работы с regex
- •Простой пример
- •Простой пример
- •Метасимволы
- •Классы символов
- •Простой класс
- •Отрицание
- •Диапазон
- •Объединения
- •Пересечения
- •Вычитание
- •Предопределенные классы
- •Примеры
- •Квантификаторы
- •Квантификаторы
- •Совпадения нулевой длины
- •Совпадения нулевой длины
- •Квантификаторы
- •Использование групп и классов с квантификаторами
- •Жадные, ленивые и захватывающие квантификаторы
- •Жадные, ленивые и захватывающие квантификаторы
- •Группы
- •Обратные ссылки
- •Граничные мета-символы (boundary matchers)
- •Граничные мета-символы (boundary matchers)
- •Перечисление
- •Методы класса Pattern
- •Методы класса Matcher
- •Методы класса Matcher
- •Методы класса Matcher
Объединения
Enter your regex: [0-4[6-8]] Enter input string to search: 0
I found the text "0" starting at index 0 and ending at index 1.
Enter your regex: [0-4[6-8]]
Enter input string to search: 5
No match found.
Enter your regex: [0-4[6-8]] Enter input string to search: 8
I found the text "8" starting at index 0 and ending at index 1.
Enter your regex: [0-4[6-8]]
Enter input string to search: 9
No match found.
Пересечения
Enter your regex: [0-9&&[345]] Enter input string to search: 3
I found the text "3" starting at index 0 and ending at index 1.
Enter your regex: [0-9&&[345]]
Enter input string to search: 6
No match found.
Enter your regex: [2-8&&[4-6]]
Enter input string to search: 3
No match found.
Enter your regex: [2-8&&[4-6]] Enter input string to search: 4
I found the text "4" starting at index 0 and ending at index 1.
Вычитание
Enter your regex: [0-9&&[^345]]
Enter input string to search: 2
I found the text "2" starting at index 0 and ending at index 1.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 3
No match found.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 4
No match found.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 5
No match found.
Предопределенные классы
. |
Любой символ |
\d |
Цифра, [0-9] |
\D |
Не цифра, [^0-9] |
\s |
Пробельный символ, [ \t\n\x0B\f\r] |
\S |
Не пробельный символ, [^\s] |
\w |
Символ слова [a-zA-Z_0-9] |
\W |
Не символ слова [^\w] |
Примеры
Enter your regex: .
Enter input string to search: @
I found the text "@" starting at index 0 and ending at index 1.
Enter your regex: \d
Enter input string to search: 1
I found the text "1" starting at index 0 and ending at index 1.
Enter your regex: \d
Enter input string to search: a
No match found.
Enter your regex: \D
Enter input string to search: 1
No match found.
Квантификаторы
Квантификатор позволяет задать количество повторений
X? |
Один или ни одного |
X* |
Ни одного, или неограниченно много |
X+ |
Один или много |
X{n} |
n повторений |
X{n,} |
Как минимум n повторений |
X{n,m} |
Как минимум n, но не больше m |
Квантификаторы
Enter your regex: a?
Enter input string to search:
I found the text "" starting at index 0 and ending at index 0.
Enter your regex: a*
Enter input string to search:
I found the text "" starting at index 0 and ending at index 0.
Enter your regex: a+
Enter input string to search:
No match found.
Совпадения нулевой длины
Enter your regex: a?
Enter input |
string to search: |
a |
I found the |
text "a" starting |
at index 0 and ending at index |
1. |
|
|
I found the |
text "" starting at index 1 and ending at index 1. |
|
Enter your regex: a* |
|
|
Enter input |
string to search: a |
|
I found the |
text "a" starting at index 0 and ending at index |
|
1. |
|
|
I found the |
text "" starting at index 1 and ending at index 1. |
|
Enter your regex: a+ |
|
|
Enter input |
string to search: a |
|
I found the |
text "a" starting at index 0 and ending at index |
|
1. |
|
|
Совпадения нулевой длины
Enter your regex: a? |
|
|
|
|
|
Enter input |
string to search: ababaaaab |
|
|
||
I found the |
text "a" starting |
at |
index 0 and ending at |
index 1. |
|
I found the |
text "" starting at index 1 and |
ending at index 1. |
|||
I found the |
text "a" starting |
at |
index 2 and ending at |
index 3. |
|
I found the |
text "" starting at index 3 and |
ending at index 3. |
|||
I found the |
text "a" starting |
at |
index 4 and ending at |
index 5. |
|
I found the |
text "a" starting |
at |
index 5 and ending at |
index 6. |
|
I found the |
text "a" starting |
at |
index 6 and ending at |
index 7. |
|
I found the |
text "a" starting |
at |
index 7 and ending at |
index 8. |
|
I found the |
text "" starting at index 8 and |
ending at index 8. |
|||
I found the |
text "" starting at index 9 and |
ending at index 9. |
|||
Enter your regex: a* |
|
|
|
|
|
Enter input |
string to search: ababaaaab |
|
|
||
I found the |
text "a" starting at |
index 0 and ending at |
index 1. |
||
I found the |
text "" starting at index 1 and |
ending at index 1. |
|||
I found the |
text "a" starting at |
index 2 and ending at |
index 3. |
||
I found the |
text "" starting at index 3 and |
ending at index 3. |
|||
I found the |
text "aaaa" starting |
at index 4 |
and ending |
at index 8. |
|
I found the |
text "" starting at index 8 and |
ending at index 8. |
|||
I found the |
text "" starting at index 9 and |
ending at index 9. |
Квантификаторы
Enter your regex: a{3}
Enter input string to search: aa
No match found.
Enter your regex: a{3}
Enter input string to search: aaa
I found the text "aaa" starting at index 0 and ending at index 3.
Enter your regex: a{3}
Enter input string to search: aaaa
I found the text "aaa" starting at index 0 and ending at index 3.
Enter your regex: a{3,}
Enter input string to search: aaaaaaaaa
I found the text "aaaaaaaaa" starting at index 0 and ending at index 9.