Я читал, что группировка рассматривает выражение как единое целое. Что это значит?
Кроме того, что делает grep "\ (grouping \)" file.txt
или grep -E "(grouping)" file.txt
? Я также читал, что выражения сгруппированы с круглыми скобками. Опять же, что такое группировка выражений?
Когда я запускаю команду, она выделяет группировку
в файле. Чем grep "\ (grouping \)"
отличается от grep grouping
?
Essentially, it causes whatever is inside the parentheses to be treated as a single atom. This is useful if you want to apply a quantifier for example. Compare:
$ printf 'groupinggrouping' | grep -Eo "grouping*"
groupingg
(where *
is only applied to the g
) with
$ printf 'groupinggrouping' | grep -Eo "(grouping)*"
groupinggrouping
where *
is applied to the whole subpattern grouping
.
In most (all?) regex dialects, (grouping)
also captures the matched text into an indexed capture group, allowing it to subsequently be backreferenced. That doesn't have too many applications in grep
- the most obvious one is for detecting repeated elements ex.
$ printf 'ba\nbb\nbc\n' | grep '\(.\)\1'
bb
matches any single character that is followed by the same character. In the context of pattern substitution (in sed
for example, rather than grep
), the captured group may also be referenced in the replacement text.
There are other variants in more expressive dialects - such as Perl's (?:grouping)
non-capturing groups.
For further information see for example