0

I have the regex ((\w;)+\w\n)(-{3,}\n)((\w;)+\w\n)+ which is suposed to match the following text:

H;e;l;a;a
---------
J;a;c;k;p

as can be seen here.

I noticed that in my regex I have a repeating part of ((\w;)+\w\n) so I tried to refactor my regex using back references.

So I got ((\w;)+\w\n)(-{3,}\n)\1+ but this one does not match the above example text. Why is this backreference is not working?

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
Jofbr
  • 455
  • 3
  • 23
  • 1
    Which language or tool are you using this in? Also backreferences are for referring to the previously matched text, not regex itself – Vasan Mar 24 '18 at 02:55
  • In some regexes, you need to mask the + sign. Others operate on complete lines and won't match a newline. – user unknown Mar 24 '18 at 03:00
  • I am using the regex in Java but am first testing the regex in regexr.com – Jofbr Mar 24 '18 at 03:03
  • @Vasan how can I get the backreference to match the previous regex? – Jofbr Mar 24 '18 at 03:04
  • Or is there any other way to remove the duplication in my initial regex? – Jofbr Mar 24 '18 at 03:04
  • Short answer: No. What you're looking for is called a regex subroutine, and isn't supported in Java. You'll need to use plain old variables to avoid duplication while building the pattern. – Vasan Mar 24 '18 at 03:22
  • would it work in kotlin? @vasan – Jofbr Mar 24 '18 at 03:26
  • Sorry, I have no idea what kotlin is. If it supports PCRE standards, it might. Try this in regexr.com after selecting PCRE from the dropdown: `((\w;)+\w[\r\n]*)(-{3,}[\r\n]*)(?1)+` – Vasan Mar 24 '18 at 03:29
  • I just checked the documentation for kotlin. If [this](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-regex/index.html) is the correct class, you're out of luck - they're just referring to Java's documentation for syntax. – Vasan Mar 24 '18 at 03:32

1 Answers1

0
((\w;)+\w)\n-{3,}\n((?1)\n)+(?1)

This seems work for this case and following text in the PCRE.

[sample text]

H;e;l;a;a
---------
J;a;c;k;p
c;d;e;d;c
f;d;e;c;d
k;d;c;d;s
d;e;f;d;e

Demo

If the sample text ends in new line(\n), then last part in this regex (?1) is not needed

Thm Lee
  • 1,236
  • 1
  • 9
  • 12
  • As mentioned in comments, this is PCRE syntax and not supported in Java or Kotlin (which OP expects). – Vasan Mar 24 '18 at 03:38
  • I missed some comments. Sorry, I edited it already with the phrase this regex is apllicable for PCRE:-) – Thm Lee Mar 24 '18 at 03:46