Make the entire match unsuccessful if conditions are not met

I am checking backtracking rollbacks and interlacing failures.

Let's say I have the following regular expression:

(foobar|barbaz)

And I run this over the vector of the following lines.

x <- c('In context I have foobar and barbaz', 'In context I have foobaz and barbaz', 'In context I have fooquz and barbaz')
regmatches(x, gregexpr('(foobar|barbaz)', x))
# [[1]]
# [1] "foobar" "barbaz"

# [[2]]
# [1] "barbaz"

# [[3]]
# [1] "barbaz"

Is there a way to make a match not to return results completely?

For example, in regex, is (foobar|barbaz)there a way to completely compensate for a complete match if the expression on the left matches only before foo, but fails if there is no after foo b? The meaning of the expression of the right side was not even considered and did not correspond.

, , fooquz foo fooquz b, , ?

+4
2

(PCRE), . PCRE perl = TRUE backtracking.

> x <- c('foobar and barbaz', 'foobaz and barbaz', 'fooquz and barbaz')
> regmatches(x, gregexpr('(foo(*COMMIT)b(*THEN)ar|barbaz)', x, perl=T))
## [[1]]
## [1] "foobar" "barbaz"

## [[2]]
## [1] "barbaz"

## [[3]]
## character(0)

, , .

> Filter(length, 
         regmatches(x, gregexpr('(foo(*COMMIT)b(*THEN)ar|barbaz)', x, perl=T)))
## [[1]]
## [1] "foobar" "barbaz"

## [[2]]
## [1] "barbaz"

  • (*COMMIT) , .
  • (*THEN) , .

  • foo , b , backtrack (*COMMIT) .

  • foo b , ar , (*THEN) barbaz.

+6

, backtracking (*COMMIT):

(?:foo(*COMMIT)bar|barbaz)

, .

0

All Articles