Match a given sequence only if not between quotes, taking escaped quotes into consideration

John Weisz

I use the below regular expression to only match a given character sequence if it is not surrounded by quotes - that is, if it is followed by an even number of quotes (using a positive lookahead) until the end of the string.

Say I want to match the word section only if it is not between quotes:

\bsection\b(?=[^"]*(?:"[^"]*"[^"]*)*$)

Working example on RegExr

How would I extend this to take escaped quotes into consideration? That is, if I insert a \" between the quotes in the linked example, the results stay the same.

Jonny 5

Using pcre could skip the quoted stuff:

(?s)".*?(?<!\\)"(*SKIP)(*F)|\bsection\b

In string regex pattern have to triple-escape the backslash, like \\\\ to match a literal backslash in the lookbehind. Or in a single quoted pattern double escaping it would be sufficient for this case.

$pattern = '/".*?(?<!\\\)"(*SKIP)(*F)|\bsection\b/s';

See test at regex101.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Selecting records with single quotes escaped by double backslashes only

분류에서Dev

What characters need to be escaped in files without quotes?

분류에서Dev

SoapUI Escaped Quotes Cause Assertion Failures

분류에서Dev

Extract everything between quotes

분류에서Dev

Regex to Match Quoted Strings Ignoring Double Quotes

분류에서Dev

Quotes within quotes

분류에서Dev

How can I efficiently switch between smart quotes and vertical quotes in Notepad++?

분류에서Dev

Vim Function: How to get text between double quotes?

분류에서Dev

Java Regex match anything that matches given pattern and is not between a given character

분류에서Dev

Double quotes in bash

분류에서Dev

String initialization with double quotes

분류에서Dev

call a function with single quotes

분류에서Dev

Limit the Number of Characters between Double Quotes using Oracle 11g PL/SQL

분류에서Dev

Limit the Number of Characters between Double Quotes using Oracle 11g PL/SQL

분류에서Dev

grep: regex only for match anything between parenthesis

분류에서Dev

How to match a word using a given character only once

분류에서Dev

Ansible replace single quotes regexp

분류에서Dev

Javascript regex for single, double and no quotes

분류에서Dev

Echoing functions, but not enough quotes available

분류에서Dev

Quotes Messing Up Python Scraper

분류에서Dev

Express req.body is in quotes

분류에서Dev

String Tokenizer (Double Quotes and Whitespace)

분류에서Dev

SQL injection without single quotes

분류에서Dev

Remove spaces (apostrophes) around quotes with regex in ruby

분류에서Dev

Dollar sign interpolation inside quotes in bash

분류에서Dev

Double quotes and variables in Sed - Unknown expression?

분류에서Dev

Ignoring a Rogue quote inside Double quotes

분류에서Dev

Regex for single quote within single quotes

분류에서Dev

Variable containing single quotes just disappears

Related 관련 기사

  1. 1

    Selecting records with single quotes escaped by double backslashes only

  2. 2

    What characters need to be escaped in files without quotes?

  3. 3

    SoapUI Escaped Quotes Cause Assertion Failures

  4. 4

    Extract everything between quotes

  5. 5

    Regex to Match Quoted Strings Ignoring Double Quotes

  6. 6

    Quotes within quotes

  7. 7

    How can I efficiently switch between smart quotes and vertical quotes in Notepad++?

  8. 8

    Vim Function: How to get text between double quotes?

  9. 9

    Java Regex match anything that matches given pattern and is not between a given character

  10. 10

    Double quotes in bash

  11. 11

    String initialization with double quotes

  12. 12

    call a function with single quotes

  13. 13

    Limit the Number of Characters between Double Quotes using Oracle 11g PL/SQL

  14. 14

    Limit the Number of Characters between Double Quotes using Oracle 11g PL/SQL

  15. 15

    grep: regex only for match anything between parenthesis

  16. 16

    How to match a word using a given character only once

  17. 17

    Ansible replace single quotes regexp

  18. 18

    Javascript regex for single, double and no quotes

  19. 19

    Echoing functions, but not enough quotes available

  20. 20

    Quotes Messing Up Python Scraper

  21. 21

    Express req.body is in quotes

  22. 22

    String Tokenizer (Double Quotes and Whitespace)

  23. 23

    SQL injection without single quotes

  24. 24

    Remove spaces (apostrophes) around quotes with regex in ruby

  25. 25

    Dollar sign interpolation inside quotes in bash

  26. 26

    Double quotes and variables in Sed - Unknown expression?

  27. 27

    Ignoring a Rogue quote inside Double quotes

  28. 28

    Regex for single quote within single quotes

  29. 29

    Variable containing single quotes just disappears

뜨겁다태그

보관