0

I am aware that this question does not make much sense, since I could just use Extended Regular Expressions (ERE). But let's say for a minute, that implementing alternation is the only option right now. How would I implement it only with BRE? Or would I have to find a workaround instead?

Let's say we have this string:

AAAAAA======-----======AAAAAAAAA

My goal now is to match AAAAA, ======, -----, ====== and AAAAAAAAA separately only with Basic Regular Expressions. With ERE this would be quickly done, but I would like to know how one would implement it only via BRE. So far, I couldn't find a proper way to do it without having some of the strings being matched overlapping into eachother (like regex matching "AAAAA==" which shouldn't happen).

  • 2
    What would be the ERE that will match as you ask? – Isaac Jun 20 at 23:12
  • @Isaac The ERE version would be: ( [A]{1,} ) | ( [=]{1,} ) | ( [-]{1,} ) – Caje Jun 20 at 23:16
  • Then: As there is no alternation (|) in BRE it is simply impossible to build a BRE regex that does the same. You can use other grep capabilities or seek a workaround, but an equivalent BRE is not possible. – Isaac Jun 21 at 16:42
1

Simple: It is not possible for an BRE to have alternation

POSIX BRE does not support any other features. Even alternation is not supported.

Your basic goal:

My goal now is to match ... separately ... with Basic Regular Expressions.

Is possible with BRE:

$ str='AAAAAA======-----======AAAAAAAAA'
$ echo "$str" | grep -Eo '(.)\1*'
AAAAAA
======
-----
======
AAAAAAAAA

Or even with several patterns in BRE grep:

$ echo "$str" | grep -o -e 'AA*' -e '--*' -e '==*'
AAAAAA
======
-----
======
AAAAAAAAA

Or restrict your solution to GNU grep (allows \| as alternation even in BRE syntax; which is not a valid POSIX feature):

$ echo "$str" | grep -o '\(AA*\)\|\(==*\)\|\(--*\)'

But your secondary goal:

is it also possible to additionally put them in separate capture groups as well?

Calls for only one regex, and as it is not possible to have alternation in BRE, that is not possible.

A very good explanation about alternation in BRE

1

To match strings of repeated characters with basic regular expressions:

$ echo 'AAAAAA======-----======AAAAAAAAA' | grep -o '\(.\)\1*'
AAAAAA
======
-----
======
AAAAAAAAA

. matches any character. \(.\) matches any character and stores that character in capture group 1. \1* matches any additional occurrences of that same character.

The use of back-references, like \1, in BRE is supported by POSIX.

  • I see. So, is it also possible to additionally put them in separate capture groups as well? – Caje Jun 21 at 0:44
  • 1
    @Caje Yes, that's possible, although it is not at all clear to me how one would benefit from doing that. What is it, exactly, that you want to accomplish? – John1024 Jun 21 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.