2012-05-26 51 views
5

वर्तमान में पानी का छींटा के लिए अनुमति देता है, मैं इस का उपयोग कर रहा:Regex अक्षरांकीय अक्षर से मेल करने, अंडरस्कोर, अवधि और पानी का छींटा, डॉट और केवल मध्य

if (preg_match ('/^[a-zA-Z0-9_]+([a-zA-Z0-9_]*[.-]?[a-zA-Z0-9_]*)*[a-zA-Z0-9_]+$/', $product)) { 
return true; 
} else { 
return false 
} 

उदाहरण के लिए, मैं मिलान करना चाहते हैं:

  1. pro.duct-name_
  2. _pro.duct.name
  3. p.r.o.d_u_c_t.n-a-m-e

लेकिन मैं मैच के लिए नहीं करना चाहती:

  1. pro..ductname
  2. .productname-
  3. -productname.
  4. -productname
+1

हो जाता है सवाल क्या है? –

+0

उदाहरणों को संपादित किया, ताकि यह अधिक समझ में आता है। क्या इसे और स्पष्टीकरण की आवश्यकता है। कृपया मुझे बताएं, मुझे आगे स्पष्टीकरण देने में खुशी होगी। – banskt

+0

'pro..ductname' मिलान क्यों नहीं होना चाहिए? डॉट्स बीच में हैं? –

उत्तर

9

जवाब

/^[a-zA-Z0-9_]+([-.][a-zA-Z0-9_]+)*$/ 

केवल आप की अनुमति दी तार मैच के लिए .- और -. युक्त नहीं करता है, तो हो सकता है। आप उन्हें क्यों मैच करने की अनुमति देंगे, वैसे भी? लेकिन अगर आप वास्तव में भी मैच के लिए इन तार की जरूरत है, एक संभव समाधान

/^[a-zA-Z0-9_]+((\.(-\.)*-?|-(\.-)*\.?)[a-zA-Z0-9_]+)*$/ 

एकल . या पहले regex के - है . और -, बारी या तो . या - के साथ शुरू की एक श्रृंखला के द्वारा बदल दिया जाता है, वैकल्पिक रूप से बारी वर्ण की समान संख्या के लिए अनुमति देने के लिए -. या .- जोड़े क्रमश: वैकल्पिक रूप से एक - या . क्रमशः द्वारा पीछा किया, जिसके बाद। यह जटिलता शायद एक ओवरहूट है, लेकिन वर्तमान विनिर्देशों की आवश्यकता होती है। 2 . और - बारी की एक अधिकतम की आवश्यकता है, regex

/^[a-zA-Z0-9_]+((\.-?|-\.?)[a-zA-Z0-9_]+)*$/ 

टेस्ट here या here

+0

दूसरा वास्तव में काम करता है। बहुत बहुत धन्यवाद, हालांकि मुझे स्वीकार करना होगा, मैं आपके दूसरे रेगेक्स के अनुक्रम को पूरी तरह से समझ नहीं पा रहा हूं। – banskt

+0

मैंने स्पष्टीकरण के लिए धन्यवाद –

+0

को स्पष्ट करने के लिए उत्तर संपादित किया। – banskt

3

इस

प्रयास करें

अद्यतन 1

(?im)^([a-z_](?:[\.\-]\w|\w)+(?![\.\-]))$ 

अद्यतन 2

(?im)^([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)$ 

स्पष्टीकरण

<!-- 
(?im)^([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)$ 

Match the remainder of the regex with the options: case insensitive (i);^and $ match at line breaks (m) «(?im)» 
Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
Match the regular expression below and capture its match into backreference number 1 «([a-z_](?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+)» 
    Match a single character present in the list below «[a-z_]» 
     A character in the range between “a” and “z” «a-z» 
     The character “_” «_» 
    Match the regular expression below «(?:\.\-\w|\-\.\w|\-\w|\.\w|\w)+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Match either the regular expression below (attempting the next alternative only if this one fails) «\.\-\w» 
     Match the character “.” literally «\.» 
     Match the character “-” literally «\-» 
     Match a single character that is a “word character” (letters, digits, and underscores) «\w» 
     Or match regular expression number 2 below (attempting the next alternative only if this one fails) «\-\.\w» 
     Match the character “-” literally «\-» 
     Match the character “.” literally «\.» 
     Match a single character that is a “word character” (letters, digits, and underscores) «\w» 
     Or match regular expression number 3 below (attempting the next alternative only if this one fails) «\-\w» 
     Match the character “-” literally «\-» 
     Match a single character that is a “word character” (letters, digits, and underscores) «\w» 
     Or match regular expression number 4 below (attempting the next alternative only if this one fails) «\.\w» 
     Match the character “.” literally «\.» 
     Match a single character that is a “word character” (letters, digits, and underscores) «\w» 
     Or match regular expression number 5 below (the entire group fails if this one fails to match) «\w» 
     Match a single character that is a “word character” (letters, digits, and underscores) «\w» 
Assert position at the end of a line (at the end of the string or before a line break character) «$» 
--> 

और आप इसे here का परीक्षण कर सकते हैं।

+1

\ w नहीं के रूप में [a-zA-Z0-9_] –

+0

परीक्षण से पता चलता है कि यह –

+0

काम नहीं करता है अगर यह @Walter क्या बात कर रहा है है मैं नहीं जानता, लेकिन थोड़ा विस्तृत करने, [एक ही है पीएचपी मैनुअल] (http://www.php.net/manual/en/regexp.reference.escape.php) का कहना है: _a "शब्द" चरित्र किसी भी अक्षर या अंक या अंडरस्कोर वर्ण, कि है, किसी भी चरित्र है जो कर सकते हैं है एक पर्ल "शब्द" का हिस्सा बनें। अक्षरों और अंकों की परिभाषा को पीसीआरई की वर्ण तालिकाओं द्वारा नियंत्रित किया जाता है, और यदि लोकेल-विशिष्ट मिलान हो रहा है तो भिन्न हो सकता है। उदाहरण के लिए, "fr" (फ्रेंच) लोकेल में, 128 से अधिक वर्ण कोड कुछ उच्चारण अक्षरों के लिए उपयोग किए जाते हैं, और इन्हें मिलान किया जाता है \ w._ – Herbert

0
/^[A-Z0-9_][A-Z0-9_.-]*[A-Z0-9_]$/i 

यह सुनिश्चित करता है कि पहला और अंतिम चरित्र डैश या अवधि नहीं है; बीच में बाकी किसी भी चरित्र (आपके चुने हुए सेट के भीतर) हो सकता है।

0

नीचे दिया गया रेगेक्स किसी भी स्ट्रिंग की जांच करेगा जिसमें अक्षर, संख्याएं, डैश इत्यादि और बीच में केवल एक बिंदु है।

/^[A-Za-z0-9_-]+(\.){1}[A-Za-z0-9_-]+$/i 

आशा है कि यह मदद करता है

1

यह करना चाहिए:

/^[A-z0-9_]([.-]?[A-Z0-9_]+)*[.-]?[A-z0-9_]$/ 

यह सुनिश्चित करें कि शब्द शुरू होता है कर देगा और अक्षरांकीय या अंडरस्कोर चरित्र के साथ समाप्त होता है। बीच में ब्रैकेट यह सुनिश्चित करेगा कि पंक्ति में अधिकतम एक अवधि या डैश होगा, इसके बाद कम से कम एक अल्फान्यूमेरिक या अंडरस्कोर वर्ण होगा।