Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to search for all occurrences in a string that are not preceded by a certain character?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading