3

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?

mkUltra
  • 2,562
  • 21
  • 42

3 Answers3

4

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
Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
  • See my [previous PHP demo](https://ideone.com/rpHGnq) with incorrect placing of apostrophes, and [the current one](https://ideone.com/4S8BnL). – Wiktor Stribiżew Sep 10 '15 at 15:05
4

Instead of splitting you can use a match using this regex:

(('[^']+)\2*)

RegEx Demo

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.

anubhava
  • 713,503
  • 59
  • 514
  • 593
0

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.

Community
  • 1
  • 1