so I am trying to match a back tick ` but only when it is not more than one in a row:
`test` // matches
``test`` // does NOT match
// does NOT match
```java
test
```
BUT it needs to also match if is at the beginning of the string or end, so all three must match.
`matches`
Text `matches`
Text `matches`EOL
UPDATE 3
The regex below matches exactly as the previous one, but consumes the backtick `
avoiding that an ending backtick is considered a starting backtick when the regex engine searchs for the next one.
(?<!`)`([^`
]+)`(?!`)
The correct behaviour (extract only the text wrapped inside single backticks) is keeped using a capturing group `([^` ]+)`
.
Use it with preg_match_all
, try this online php demo.
Legenda:
(?<!`)`
a backtick not preceded another one`([^` ]+)`
a capturing group that matches everything that is not a backtick or a newline (CR or LF)`(?!`)
a backtick not followed by another one