Search code examples
c#regexlookbehindnegative-lookbehind

A regex problem I can't figure out (negative lookbehind)


how do i do this with regex?

i want to match this string: -myString

but i don't want to match the -myString in this string: --myString

myString is of course anything.

is it even possible?

EDIT:

here's a little more info with what i got so far since i've posted a question:

string to match:
some random stuff here -string1, --string2, other stuff here
regex:
(-)([\w])*

This regex returns me 3 matches: -string1, - and -string2

ideally i'd like it to return me only the -string1 match


Solution

  • Assuming your regex engine supports (negative) lookbehind:

    /(?<!-)-myString/
    

    Perl does, Javascript doesn't, for example.