This commit is contained in:
Ilya Kantor 2017-03-20 15:26:18 +03:00
parent 91e4c89773
commit 1e2b09b6fb
12 changed files with 156 additions and 100 deletions

View file

@ -1,20 +0,0 @@
Открывающий тег -- это `pattern:\[(b|url|quote)\]`.
Для того, чтобы найти всё до закрывающего -- используем ленивый поиск `pattern:[\s\S]*?` и обратную ссылку на открывающий тег.
Итого, получится: `pattern:\[(b|url|quote)\][\s\S]*?\[/\1\]`.
В действии:
```js run
var re = /\[(b|url|quote)\][\s\S]*?\[\/\1\]/g;
var str1 = "..[url]http://ya.ru[/url]..";
var str2 = "..[url][b]http://ya.ru[/b][/url]..";
alert( str1.match(re) ); // [url]http://ya.ru[/url]
alert( str2.match(re) ); // [url][b]http://ya.ru[/b][/url]
```
Для закрывающего тега `[/1]` понадобилось дополнительно экранировать слеш: `\[\/1\]`.

View file

@ -1,41 +0,0 @@
# Найдите пары тегов
ББ-тег имеет вид `[имя]...[/имя]`, где имя -- слово, одно из: `b`, `url`, `quote`.
Например:
```
[b]текст[/b]
[url]http://ya.ru[/url]
```
ББ-теги могут быть вложенными, но сам в себя тег быть вложен не может, например:
```
Допустимо:
[url] [b]http://ya.ru[/b] [/url]
[quote] [b]текст[/b] [/quote]
Нельзя:
[b][b]текст[/b][/b]
```
Создайте регулярное выражение для поиска ББ-тегов и их содержимого.
Например:
```js
var re = /* регулярка */
var str = "..[url]http://ya.ru[/url]..";
alert( str.match(re) ); // [url]http://ya.ru[/url]
```
Если теги вложены, то нужно искать самый внешний тег (при желании можно будет продолжить поиск в его содержимом):
```js
var re = /* регулярка */
var str = "..[url][b]http://ya.ru[/b][/url]..";
alert( str.match(re) ); // [url][b]http://ya.ru[/b][/url]
```

View file

@ -1,63 +1,62 @@
# Обратные ссылки: \n и $n
# Backreferences: \n and $n
Скобочные группы можно не только получать в результате.
Движок регулярных выражений запоминает их содержимое, и затем его можно использовать как в самом паттерне, так и в строке замены.
Capturing groups may be accessed not only in the result, but in the replacement string, and in the pattern too.
[cut]
## Группа в строке замены
## Group in replacement: $n
Ссылки в строке замены имеют вид `$n`, где `n` -- это номер скобочной группы.
When we are using `replace` method, we can access n-th group in the replacement string using `$n`.
Вместо `$n` подставляется содержимое соответствующей скобки:
For instance:
```js run
var name = "Александр Пушкин";
let name = "John Smith";
name = name.replace(/([а-яё]+) ([а-яё]+)/i, *!*"$2, $1"*/!*);
alert( name ); // Пушкин, Александр
name = name.replace(/(\w+) (\w+)/i, *!*"$2, $1"*/!*);
alert( name ); // Smith, John
```
В примере выше вместо `pattern:$2` подставляется второе найденное слово, а вместо `pattern:$1` -- первое.
Here `pattern:$1` in the replacement string means "substitute the content of the first group here", and `pattern:$2` means "substitute the second group here".
## Группа в шаблоне
Referencing a group in the replacement string allows us to reuse the existing text during the replacement.
Выше был пример использования содержимого групп в строке замены. Это удобно, когда нужно реорганизовать содержимое или создать новое с использованием старого.
## Group in pattern: \n
Но к скобочной группе можно также обратиться в самом поисковом шаблоне, ссылкой вида `\номер`.
A group can be referenced in the pattern using `\n`.
Чтобы было яснее, рассмотрим это на реальной задаче -- необходимо найти в тексте строку в кавычках. Причём кавычки могут быть одинарными `subject:'...'` или двойными `subject:"..."` -- и то и другое должно искаться корректно.
To make things clear let's consider a task. We need to find a quoted string: either a single-quoted `subject:'...'` or a double-quoted `subject:"..."` -- both variants need to match.
Как такие строки искать?
How to look for them?
Можно в регэкспе предусмотреть произвольные кавычки: `pattern:['"](.*?)['"]`. Такой регэксп найдёт строки вида `match:"..."`, `match:'...'`, но он даст неверный ответ в случае, если одна кавычка ненароком оказалась внутри другой, как например в строке `subject:"She's the one!"`:
We can put two kinds of quotes in the pattern: `pattern:['"](.*?)['"]`. That finds strings like `match:"..."` and `match:'...'`, but it gives incorrect matches when one quote appears inside another one, like the string `subject:"She's the one!"`:
```js run
var str = "He said: \"She's the one!\".";
let str = "He said: \"She's the one!\".";
var reg = /['"](.*?)['"]/g;
let reg = /['"](.*?)['"]/g;
// Результат не соответствует замыслу
// The result is not what we expect
alert( str.match(reg) ); // "She'
```
Как видно, регэксп нашёл открывающую кавычку `match:"`, затем текст, вплоть до новой кавычки `match:'`, которая закрывает соответствие.
As we can see, the pattern found an opening quote `match:"`, then the text is consumed lazily till the other quote `match:'`, that closes the match.
Для того, чтобы попросить регэксп искать закрывающую кавычку -- такую же, как открывающую, мы обернём её в скобочную группу и используем обратную ссылку на неё:
To make sure that the pattern looks for the closing quote exactly the same as the opening one, let's make a group of it and use the backreference:
```js run
var str = "He said: \"She's the one!\".";
let str = "He said: \"She's the one!\".";
var reg = /(['"])(.*?)\1/g;
let reg = /(['"])(.*?)\1/g;
alert( str.match(reg) ); // "She's the one!"
```
Теперь работает верно! Движок регулярных выражений, найдя первое скобочное выражение -- кавычку `pattern:(['"])`, запоминает его и далее `pattern:\1` означает "найти то же самое, что в первой скобочной группе".
Now everything's correct! The regular expression engine finds the first quote `pattern:(['"])` and remembers the content of `pattern:(...)`, that's the first capturing group.
Обратим внимание на два нюанса:
Further in the pattern `pattern:\1` means "find the same text as in the first group".
- Чтобы использовать скобочную группу в строке замены -- нужно использовать ссылку вида `$1`, а в шаблоне -- обратный слэш: `\1`.
- Чтобы в принципе иметь возможность обратиться к скобочной группе -- не важно откуда, она не должна быть исключена из запоминаемых при помощи `?:`. Скобочные группы вида `(?:...)` не участвуют в нумерации.
Please note:
- To reference a group inside a replacement string -- we use `$1`, while in the pattern -- a backslash `\1`.
- If we use `?:` in the group, then we can't reference it. Groups that are excluded from capturing `(?:...)` are not remembered by the engine.

View file

@ -0,0 +1,23 @@
Opening tag is `pattern:\[(b|url|quote)\]`.
Then to find everything till the closing tag -- let's the pattern `pattern:[\s\S]*?` to match any character including the newline and then a backreference to the closing tag.
The full pattern: `pattern:\[(b|url|quote)\][\s\S]*?\[/\1\]`.
In action:
```js run
let reg = /\[(b|url|quote)\][\s\S]*?\[\/\1\]/g;
let str = `
[b]hello![/b]
[quote]
[url]http://google.com[/url]
[/quote]
`;
alert( str.match(reg) ); // [b]hello![/b],[quote][url]http://google.com[/url][/quote]
```
Please note that we had to escape a slash for the closing tag `pattern:[/\1]`, because normally the slash closes the pattern.

View file

@ -0,0 +1,48 @@
# Find bbtag pairs
A "bb-tag" looks like `[tag]...[/tag]`, where `tag` is one of: `b`, `url` or `quote`.
For instance:
```
[b]текст[/b]
[url]http://google.com[/url]
```
BB-tags can be nested. But a tag can't be nested into itself, for instance:
```
Normal:
[url] [b]http://google.com[/b] [/url]
[quote] [b]text[/b] [/quote]
Impossible:
[b][b]text[/b][/b]
```
Tags can contain line breaks, that's normal:
```
[quote]
[b]text[/b]
[/quote]
```
Create a regexp to find all BB-tags with their contents.
For instance:
```js
let reg = /your regexp/g;
let str = "..[url]http://google.com[/url]..";
alert( str.match(reg) ); // [url]http://google.com[/url]
```
If tags are nested, then we need the outer tag (if we want we can continue the search in its content):
```js
let reg = /your regexp/g;
let str = "..[url][b]http://google.com[/b][/url]..";
alert( str.match(reg) ); // [url][b]http://google.com[/b][/url]
```

View file

@ -1,25 +1,72 @@
# Альтернация (или) |
# Alternation (OR) |
Альтернация -- термин в регулярных выражениях, которому в русском языке соответствует слово "ИЛИ". Она обозначается символом вертикальной черты `pattern:|` и позволяет выбирать между вариантами.
Alternation is the term in regular expression that is actually a simple "OR".
In a regular expression it is denoted with a vertial line character `pattern:|`.
[cut]
Например, нам нужно найти языки программирования: HTML, PHP, Java и JavaScript.
For instance, we need to find programming languages: HTML, PHP, Java or JavaScript.
Соответствующее регулярное выражение: `pattern:html|php|java(script)?`.
The corresponding regexp: `pattern:html|php|java(script)?`.
Пример использования:
A usage example:
```js run
var reg = /html|php|css|java(script)?/gi
let reg = /html|php|css|java(script)?/gi;
var str = "Сначала появился HTML, затем CSS, потом JavaScript"
let str = "First HTML appeared, then CSS, then JavaScript";
alert( str.match(reg) ) // 'HTML', 'CSS', 'JavaScript'
alert( str.match(reg) ); // 'HTML', 'CSS', 'JavaScript'
```
Мы уже знаем похожую вещь -- квадратные скобки. Они позволяют выбирать между символами, например `pattern:gr[ae]y` найдёт `match:gray`, либо `match:grey`.
We already know a similar thing -- square brackets. They allow to choose between multiple character, for instance `pattern:gr[ae]y` matches `match:gray` or `match:grey`.
Альтернация работает уже не посимвольно, а на уровне фраз и подвыражений. Регэксп `pattern:A|B|C` обозначает поиск одного из выражений: `A`, `B` или `C`, причём в качестве выражений могут быть другие, сколь угодно сложные регэкспы.
Alternation works not on a character level, but on expression level. A regexp `pattern:A|B|C` means one of expressions `A`, `B` or `C`.
Для указания границ альтернации используют скобки `(...)`, например: `pattern:before(XXX|YYY)after` будет искать `match:beforeXXXafter` или `match:beforeYYYafter`.
For instance:
- `pattern:gr(a|e)y` means exactly the same as `pattern:gr[ae]y`.
- `pattern:gra|ey` means "gra" or "ey".
To separate a part of the pattern for alternation we usually enclose it in parentheses, like this: `pattern:before(XXX|YYY)after`.
## Regexp for time
In previous chapters there was a task to build a regexp for searching time in the form `hh:mm`, for instance `12:00`. But a simple `pattern:\d\d:\d\d` is too vague. It accepts `25:99` as the time.
How can we make a better one?
We can apply more careful matching:
- The first digit must be `0` or `1` followed by any digit.
- Or `2` followed by `pattern:[0-3]`
As a regexp: `pattern:[01]\d|2[0-3]`.
Then we can add a colon and the minutes part.
The minutes must be from `0` to `59`, in the regexp language that means the first digit `pattern:[0-5]` followed by any other digit `\d`.
Let's glue them together into the pattern: `pattern:[01]\d|2[0-3]:[0-5]\d`.
We're almost done, but there's a problem. The alternation `|` is between the `pattern:[01]\d` and `pattern:2[0-3]:[0-5]\d`. That's wrong, because it will match either the left or the right pattern:
```js run
let reg = /[01]\d|2[0-3]:[0-5]\d/g;
alert("12".match(reg)); // 12 (matched [01]\d)
```
That's rather obvious, but still an often mistake when starting to work with regular expressions.
We need to add parentheses to apply alternation exactly to hours: `[01]\d` OR `2[0-3]`.
The correct variant:
```js run
let reg = /([01]\d|2[0-3]):[0-5]\d/g;
alert("00:00 10:10 23:59 25:99 1:2".match(reg)); // 00:00,10:10,23:59
```