Note that this question is in the context of Julia, and therefore (as far as I know) PCRE.
Suppose you had a line like this:
"sssppaaasspaapppssss"
and you would like to individually combine the repeated characters at the end of the line (in the case of our line, the four characters "s" - that is, so that matchall returns ["s", "s", "s", "s"], and not ["ssss"]). This is easy:
r"(.)(?=\1*$)"
This is almost trivial (and easy to use - replace(r"(.)(?=\1*$)","hell","k") will give "hekk" , and replace(r"(.)(?=\1*$)","hello","k") will give "hellk" ). And it can be generalized to repeat patterns by disabling the dot for something more complex:
r"(\S+)(?=( \1)*$)"
which, for example, will independently match the last three instances of "abc" in "abc abc defg abc h abc abc abc" .
Which then leads to the question ... how would you fit a repeating character or pattern at the beginning of a line? In particular, the use of a regular expression in the form in which it was used above.
An obvious approach would be to change the direction of the aforementioned regular expression as r"(?<=^\1*)(.)" - but PCRE / Julia does not allow lookbehinds to have a variable length (except when it is fixed-variable, for example (?<=ab|cde) ), and thus produces an error. The next thought is to use "\ K" as something in the r"^\1*\K(.)" lines r"^\1*\K(.)" , But this is only possible to match the first character (apparently because it "is being promoted "after matching it and no longer matches the carriage).
For clarity: I'm looking for a regular expression that, for example, will result in
replace("abc abc defg abc h abc abc abc",<regex here>,"hello")
production
"hello hello defg abc h abc abc abc"
As you can see, it replaces each βabcβ from the very beginning βhelloβ, but only until the first inconsistency. The converse, which I set out above, does this at the other end of the line:
replace("abc abc defg abc h abc abc abc",r"(\S+)(?=( \1)*$)","hello")
produces
"abc abc defg abc h hello hello hello"