tags:

views:

59

answers:

3

Hi folks,

pretty simple -> a regex query to find two dashes in a string. If a string has more than two dashes, then it returns the index of the first two dashes.

eg.

PASS: fdhfdjkgsdf--sdfdsfsdsf
PASS: sldfjkdsf------dsfdsf----fds
FAIL: asdasdsa
FAIL: asd-dsadsaads
+4  A: 

You don't need a regex for this, most languages have an indexOf or similar that looks for a literal string and returns the index of the first occurrence. But if you need a regex, it's probably just -- (depending on your flavor of regex), because outside of a [] construct, - doesn't have a special meaning.

JavaScript examples (you didn't mention what language you were using):

// Find the index of the first occurrence of "--":
if ((n = theString.indexOf("--")) >= 0) {
    // It contains them starting at index 'n'
}

// Using string.match, which checks a string to see if it contains
// a match for a regex:
if (theString.match(/--/)) {
    // Yes it does
}
T.J. Crowder
+2  A: 

"-" isn't a special character unless it's in a range, so your regex is just "--".

David Kanarek
+1  A: 

if you want to use regex, its as simple as /--/. however, most languages provide string methods to find index of a substring, eg in Python.

>>> s="abc--def"
>>> s.index("--")
3
>>> s.find("--")
3

In Perl, you can use index()

ghostdog74