How to use striping with lookbehind

Purpose:

I would like to match sentences with the word "no", but only if "no" is not preceded by "c" or "is" or "is" in r.

Input:

The ground was rocky with no cracks in it
No diggedy, no doubt
Understandably, there is no way an elephant can be green

Expected Result:

The ground was rocky with no cracks in it
Understandably, there is no way an elephant can be green

Attempt

gsub(".*(?:((?<!with )|(?<!there is )|(?<!there are ))\\bno\\b(?![?:A-Za-z])|([?:]\\s*N?![A-Za-z])).*\\R*", "", input_string, perl=TRUE, ignore.case=TRUE)

Problem:

The negative lookbehind seems to be ignored, so that all sentences are replaced. Is the problem using striping in a lookbehind statement?

+6
source share
2 answers

you can use

(?mxi)^       # Start of a line (and free-spacing/case insensitive modes are on)
(?:           # Outer container group start
  (?!.*\b(?:with|there\h(?:is|are))\h+no\b) # no 'with/there is/are no' before 'no'
  .*\bno\b  # 'no' whole word after 0+ chars
  (?![?:])    # cannot be followed with ? or :
|             # or
  .*          # any 0+ chars
  [?:]\h*n(?![a-z]) # ? or : followed with 0+ spaces, 'n' not followed with any letter
)             # container group end
.*            # the rest of the line and 
\R*           # 0+ line breaks

regex. : 2 , , no , with, there is there are , , ? :, 0+ (\h), n .

R demo:

sentences <- "The ground was rocky with no cracks in it\r\nNo diggedy, no doubt\r\nUnderstandably, there is no way an elephant can be green"
rx <- "(?mxi)^ # Start of a line
(?:            # Outer container group start
  (?!.*\\b(?:with|there\\h(?:is|are))\\h+no\\b) # no 'with/there is/are no' before 'no'
  .*\\bno\\b   # 'no' whole word after 0+ chars
  (?![?:])     # cannot be followed with ? or :
|              # or
  .*           # any 0+ chars
  [?:]\\h*n(?![a-z]) # ? or : followed with 0+ spaces, 'n' not followed with any letter
)              # container group end
.*             # the rest of the line and 0+ line breaks
\\R*"
res <- gsub(rx, "", sentences, perl=TRUE)
cat(res, sep="\n")

:

The ground was rocky with no cracks in it
Understandably, there is no way an elephant can be green

x regex . , \\h ( ), \\s ( ), \\n (LF), \\r (CR) .., .

(?i) ingore.case=TRUE.

+2

. , "" . \\1, .. .

gsub("(?i)(.*(with|there (?:is|are)) no\\b.*)|.*", "\\1" ,string, perl=T)

DEMO

:

x <- "The ground was rocky with no cracks in it\nNo diggedy, no doubt\nUnderstandably, there is no way an elephant can be green"
gsub("(?i)(.*(with|there (?:is|are)) no\\b.*\\n?)|.*\\n?", "\\1" ,x, perl=T)
# [1] "The ground was rocky with no cracks in it\nUnderstandably, there is no way an elephant can be green"
+3

All Articles