string time = "Job started: donderdag 6 mei 2010 at 20:00:02"
var filterReg = new Regex(@".*:", RegexOptions.Compiled);
time = filterReg.Replace(time, String.Empty);
Is it possible to stop after the first occurence? so at the first ":".
您使用正则表达式获取简单子字符串的任何原因是什么?
time = time.Substring(time.IndexOf(":") + 1);
By using a more specific regex
new Regex(@"^[^:]*:", RegexOptions.Compiled);
Your .*:
does this
.*
matches everything, greedily, so it runs right to the end of the string. :
tries to match, so the regex engine goes back one character at a time (this is called backtracking) to find a match. It stops at the first colon it finds (seen from the end of the string) whereas ^[^:]*:
does this:
^
anchors the regex to the start of the string. no matches in the middle of the string can occur. [^:]*
matches everything except colons, greedily, so it runs right to the first colon :
can match easily, because the next character happens to be a colon. Done. No backtracking involved, this means it is also more efficient.
Use this regex .*?:
with the Replace
overload :
string time = "Job started: donderdag 6 mei 2010 at 20:00:02"
var filterReg = new Regex(@".*?:", RegexOptions.Compiled);
filterReg.Replace(time, String.Empty, 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.