Home > Back-end >  How to search for all occurrences in a string that are not preceded by a certain character?
How to search for all occurrences in a string that are not preceded by a certain character?

Time:11-13

I have the following string dataOne[1][2][3]./../dataTwo[4][5][6].dataThree/./dataFour and I want to find all occurrences of the sub string ./ that are not preceded by a . or /.

I wrote the following reg. ex. (?!(\/|\.))\.\/ and I expected to match only the first occurrences of the ./ sub string that is located after the dataOne[1][2][3] sub string, but it doesn't work: does anyone know where the problem is?

CodePudding user response:

Negative lookbehinds are specified using (?<!), not (?!), the latter which is for negative lookaheads. Also, you can use a character class to express the characters which you want to blacklist. Putting this all together, you may use the following regex pattern:

(?<![./])\./

Demo

  • Related