hhamm
hhamm

Reputation: 1571

Regular expressen that matches / but not //

For a custom syntax highlighting in "Sublime Text 2" I need a solution to regex the devisor "/" but not the double-slash comment "//"

example code for regex matching could look like this:

void main()
{
    int a = 10/2; // simple devision
    //        ^    ^ 
    //        |    +-- comment (the regex below will match the second slash)
    //        +-- devisor
}

my current regex looks like:

\/(?!\/)

The result of this regex matches also the second slash in the comment. How could I define a regex that only matches the devisor but does not touch the double slash comment at all?

Upvotes: 0

Views: 41

Answers (2)

Use a non-matching character class:

[^\/]*(\/)[^\/]*

This will match any / that is not preceded or followed by another /.

Upvotes: 0

Sam Choukri
Sam Choukri

Reputation: 1904

You were close. You specified a negative-lookahead assertion that a slash cannot follow a slash. And now you also need to specify a negative-lookbehind assertion that a slash cannot precede a slash.

(?<!\/)\/(?!\/)

Upvotes: 1

Related Questions