开发者

What regex can I use to find two dashes? ie. --

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


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
}


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


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()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜