I have string "'a'a'a'a'b'c'c'a'a'd'e'e'e'e" and I need create an array of strings in this mode:
["'a'a'a'a'", "b'", "c'c'", "a'a'" ,"d'", "e'e'e'e"]
How can I get it with regex?
I have string "'a'a'a'a'b'c'c'a'a'd'e'e'e'e" and I need create an array of strings in this mode:
["'a'a'a'a'", "b'", "c'c'", "a'a'" ,"d'", "e'e'e'e"]
How can I get it with regex?
You can match them with
(?:^')?(.')\1*(?:.$)?
See regex demo
The regex matches optional '
at the beginning with (?:^')?
, then matches and captures any symbol other than a newline followed by a '
(with (.')
), followed by itself any number of times (with \1*
) and then followed by an optional any symbol but a newline at the end of the string (with (?:.$)?
).
Output:
'a'a'a'a'
b'
c'c'
a'a'
d'
e'e'e'e
Instead of splitting you can use a match using this regex:
(('[^']+)\2*)
This will match a single quote followed by 1 or more non-single-quote character and group it. Later a back-reference can be used to match 0 or more occurrences of captured string.
This regex uses lookarounds. In the lookbehind it captures and looks ahead.
(?<=(\w)'(?!\1))
Matches right after '
if between different characters and left is a word character.