Hãy thử điều này:
(?-i)^[a-z0-9_-]+$(?#case sensitive, matches only lower a-z)
hoặc
(?i)^[a-z0-9_-]+$(?#case insensitive, matches lower and upper letters)
mẫu mã
try {
Regex regexObj = new Regex("^[a-z0-9_-]+$(?#case sensitive, matches only lower a-z)", RegexOptions.Multiline);
Match matchResults = regexObj.Match(subjectString);
while (matchResults.Success) {
for (int i = 1; i < matchResults.Groups.Count; i++) {
Group groupObj = matchResults.Groups[i];
if (groupObj.Success) {
// matched text: groupObj.Value
// match start: groupObj.Index
// match length: groupObj.Length
}
}
matchResults = matchResults.NextMatch();
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
regex giải phẫu
// (?-i)^[a-z0-9_-]+$(?#case sensitive, matches only lower a-z)
//
// Options:^and $ match at line breaks
//
// Match the remainder of the regex with the options: case sensitive (-i) «(?-i)»
// Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
// Match a single character present in the list below «[a-z0-9_-]+»
// Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// A character in the range between “a” and “z” «a-z»
// A character in the range between “0” and “9” «0-9»
// The character “_” «_»
// The character “-” «-»
// Assert position at the end of a line (at the end of the string or before a line break character) «$»
// Comment: case sensitive, matches only lower a-z «(?#case sensitive, matches only lower a-z)»
lý do bỏ phiếu xuống? xin vui lòng bình luận để tôi có thể cải thiện thời gian tới –
Bởi vì không có nỗ lực đã được thực hiện một phần của bạn để giải quyết vấn đề. Về cơ bản, bạn hỏi "làm việc này cho tôi". – Blender
Tôi đã tìm thấy nhiều chủ đề ngăn xếp và blog nhưng tất cả đều không hoạt động trong trường hợp của tôi và tôi không thể chỉnh sửa regex vì tôi không có ý tưởng về regex. dù sao thì tôi cũng sẽ giải quyết vấn đề này lần sau. –