2013-02-02 14 views
6

मैं अंग्रेज़ी अक्षर, हाइफन के लिए regex और रेखांकितRegex और रेखांकित

उदाहरण

मैच की जरूरत है:

 
govind-malviya 
govind_malviya 
govind123 
govind 

 
govind malviya 
govind.malviya 
govind%malviya 
腕錶生活 
вкусно-же 
+0

वोट क्यों नीचे? कृपया टिप्पणी करें ताकि मैं अगली बार –

+0

में सुधार कर सकूं क्योंकि समस्या को हल करने के लिए आपके हिस्से पर कोई प्रयास नहीं किया गया था। आप मूल रूप से पूछ रहे हैं "मेरे लिए यह काम करें"। – Blender

+0

मुझे विभिन्न स्टैक थ्रेड और ब्लॉग मिले हैं लेकिन सभी मेरे मामले में काम नहीं कर रहे हैं और मैं रेगेक्स को संपादित नहीं कर सका क्योंकि मुझे रेगेक्स के बारे में कोई जानकारी नहीं है। वैसे भी धन्यवाद अगली बार मैं इस मामले का ख्याल रखूंगा। –

उत्तर

15

इस आजमा कर देखें:

^[A-Za-z\d_-]+$ 

A-Za-z अक्षर की अनुमति होगी।
\d संख्याओं की अनुमति देगा।
_ अंडरस्कोर की अनुमति देगा।
- हाइफ़न की अनुमति देगा। ^ और $ क्रमशः स्ट्रिंग की शुरुआत और अंत का प्रतिनिधित्व करते हैं।

-1

[\w-]+ इस से मेल नहीं आपको क्या चाहिए
\w शब्द चरित्र है। यह [a-zA-Z1-9_] जैसा है जिसका अर्थ a से z या A से Z या 1 से 9 या अंडरस्कोर से होता है। तो [\w-] का अर्थ एक शब्द चरित्र या एक हाइफ़न है।
+ का मतलब है एक या अधिक बार

+0

क्या यह हाइफ़न (-) के लिए मान्य है? –

+0

यह उत्तर http://msdn.microsoft.com/en-us/library/20bw873z.aspx#WordCharacter – dtb

+0

के अनुसार गलत है मैंने संपादित किया है '[\ w -] + 'पैटर्न – shift66

0

इस प्रयास करें:

(?-i)^[a-z0-9_-]+$(?#case sensitive, matches only lower a-z) 

या

(?i)^[a-z0-9_-]+$(?#case insensitive, matches lower and upper letters) 

नमूना कोड

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 शरीर रचना विज्ञान

// (?-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)»