هل هناك أداة من شأنها تحويل POSIX إلى PCRE لـ PHP؟[ينسخ]

StackOverflow https://stackoverflow.com/questions/1023227

سؤال

هذا السؤال لديه بالفعل إجابة هنا:

هل هناك أداة من شأنها تحويل POSIX إلى PCRE لـ PHP؟أنا في حيرة إلى حد ما من دليل PHP الخاص بـ PCRE، وبينما سأحاول العثور على مزيد من المعلومات حول PCRE، كنت أتساءل عما إذا كان أي شخص قد صمم مثل هذه الأداة المساعدة.

أو إذا كان أي شخص يشرح كيفية تحويل ما يلي، فسيكون ذلك جيدًا أيضًا:

ereg("^#[01-9A-F]{6}$", $sColor)

لكن من فضلك اشرح لي كيف يتم ذلك، وليس فقط أخبرني بالتحويل.

هل كانت مفيدة؟

المحلول

preg_match("/^#[01-9A-F]{6}$/", $sColor)
In this case you only need to add the two delimiters.

في بيرل يمكنك كتابة شيء من هذا القبيل

if ( s =~ /x.+y/ )
{ print "match"; }
As you can see the actual regular expression is encapsulated in //. If you want to set an option on the regular expression you put it after the second /, e.g. switching the expression to ungreedy by default /x.+y/U
pcre now emulates this behaviour. Though you have to call a function you also have to provide the delimiters and set the options after the second delimiter. In perl the delimiter has to be /, with pcre you can chose more freely
preg_match("/^#[01-9A-F]{6}$/", $sColor)
preg_match("!^#[01-9A-F]{6}$!", $sColor)
preg_match("#^\#[01-9A-F]{6}$#", $sColor) // need to escape the # within the expression here
preg_match("^#[01-9A-F]{6}$", $sColor)
كل نفس بالنسبة إلى pcre، من الأفضل اختيار حرف لا يظهر في التعبير.

نصائح أخرى

preg_match("/^#[01-9A-F]{6}$/D", $sColor)

لاحظ ال D المعدل.ينسى الناس ذلك طوال الوقت.بدونه $ سيسمح بحرف السطر الجديد النهائي.سيتم تمرير سلسلة مثل "#000000 ".هذا فرق دقيق بين POSIX وPCRE.

وبالطبع، [01-9] يمكن إعادة كتابتها ل [0-9].

بالمناسبة، PHP تدعم التعبيرات العادية PCRE وPOSIX.فيما يلي قسم دليل PHP الخاص بالتعبيرات العادية POSIX، لذلك لا يتعين عليك تحويلها: http://www.php.net/manual/en/book.regex.php

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top