I want to replace a string, say “000000” to the letter L only if it comes after a multiple of six digits with regex. I am using Siri Shortcuts on iOS, and my project is a system managing binary strings. My current method is to split the base string into six-byte chunks, then to run my regex’s, but this is not as efficient as it can be and it crashes with larger strings.
For example:
Input: “000000010110100000000000”
Split for human reading: “000000-010110-100000-000000”
Desired output: “L010110100000L”
I’ve tried notations such as (?=(\d{6})*)000000
, as well as replacing *
with +
, changing groups, writing \d
six times instead of \d{6}
and several other things, but I can’t figure out how to force it to a multiple of six. Usually, I get an output such as:
“L0101101L00000” instead of “L010110100000L”
Is there a way to ensure that there is a multiple of six digits (not characters) behind the string I want to replace? Alternatively, is there a way to put a character such as a space after every sixth character without removing the characters? Siri Shortcuts uses the ICU flavor of regex.
If I understand your first requirement correctly, you could use something like this:
(\G(?:\d{6})*?)(000000)
And replace it with:
$1L
Demo.
For this part:
Alternatively, is there a way to put a character such as a space after every sixth character without removing the characters?
..you may use something like:
(\d{6})(?!$)
..and then replace it with \$
followed by the character you want to add. For example \$-
.
Demo.