Can you use backlinks in the regex pattern part?

Is there a way to backlink in a regex pattern?

Example input line:

Here is "some quoted" text.

Say I want to pull out the quoted text, I could create the following expression:

"([^"]+)"

This regular expression will match some quoted.

Say I want it to support single quotes, I could change the expression to:

["']([^"']+)["']

But what if the input string contains a mixture of quotes, say Here is 'some quoted" text., I would not want the regular expression to match. Currently, the regex in the second example will still match.

, , , - , . - , .

?


:

+3
3

:

(["'])[^"']+\1
  • ():
  • [..]: char. ["'] ", ' "|'
  • [^..]: char . char, ^
  • +:
  • \1: , (["'])

PHP :

preg_match('#(["\'])[^"\']+\1#',$str)

+4
preg_match('/(["\'])([^"\']+)\1/', 'Here is \'quoted text" some quoted text.');

: (["'])([^"']+)\1/ . , 1. , , \1, , 1.

+2

/"\(.*?\)".*?\1/ ,

0

Source: https://habr.com/ru/post/1743009/


All Articles