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,828
  • 1
  • 22
  • 47

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
  • 607,720
  • 39
  • 448
  • 563
  • 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
  • 761,203
  • 64
  • 569
  • 643
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