ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programming[ Regular Expression ] มาแชร์ประสบการณ์กัน
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: [ Regular Expression ] มาแชร์ประสบการณ์กัน  (อ่าน 3510 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« เมื่อ: 14 พฤษภาคม 2009, 01:35:18 »

คือว่ามันไม่มีหนังสือสอนทางนี้โดยเฉพาะแบบละเอียดครับแล้ว
ภาษาอังกฤษผมก็งู ๆ ปลา ๆ ทำให้ไม่ค่อยก้าวหน้าเลย
ผมคิดว่ายังมีอีกหลายคนที่เป็นแบบผมก็เลยลองตั้งกระทู้เกี่ยวกับเรื่องนี้โดยเฉพาะครับ

คำถาม 1
pattern modiflers แต่ละตัวมีความหมายอย่างไรและสมควรใช้ในกรณีไหนครับ
คือแบบเนี้ยอ่ะครับ preg_match("#pattern#isu", $data, $match)
g คือ .....
i คือ .....
m คือ .....
s คือ .....
x คือ .....
e คือ .....
U คือ .....
isu คือ .....

หัวข้อ 2 : เทคนิคและความเข้าใจในการเขียน pattern
ผมสงสัยและสังเกตุมานานครับว่าบาง pattern นั้นเขียนแบบเข้าใจได้ยากมาก ๆ (หรือผมโง่เองก็ไม่รู้)
ถ้าหากท่านใดพออธิบายความหมายของ pattern เหล่านี้ได้ก็ช่วย ๆ กันทีนะครับ
หรือจะลองยก pattern ที่ท่านคิดว่าแปลก ๆ และเข้าใจได้ยาก ถ้าเรียนรู้ด้วยตนเองมาก็ได้ครับ

1. <[^">]+"?([^"]+)"?[^>]+>
2. >([^<]*)<
3. /\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si
« แก้ไขครั้งสุดท้าย: 14 พฤษภาคม 2009, 01:47:37 โดย waterandthenbrokenparting » บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
peglui
คนรักเสียว
*

พลังน้ำใจ: 4
ออฟไลน์ ออฟไลน์

กระทู้: 180



ดูรายละเอียด
« ตอบ #1 เมื่อ: 14 พฤษภาคม 2009, 01:44:16 »

เคยใช้แต่ s กับ i อา

i - ไม่สน ตัวเล็กตัวใหญ่ ในภาษาอังกฤษ
s - จับ pattern โดยจะมองทุกอักขระ เป็น ต้นข้อมูลหมด (คือปกติเราไว้ใช้เวลาให้มันจับข้อมูลที่อาจอยู่ข้ามบรรทัดอะ เพราะปกติมันจะไม่กินยาวข้ามบรรทัด)



- -'' ถูกผิดอย่างไง ช่วยแนะนำด้วยเน้อ งู ๆ ปลา ๆ เหมือนกัน  Cry Cry Cry
บันทึกการเข้า
pugkung
Verified Seller
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 196
ออฟไลน์ ออฟไลน์

กระทู้: 2,681



ดูรายละเอียด เว็บไซต์
« ตอบ #2 เมื่อ: 14 พฤษภาคม 2009, 01:50:38 »

ผมเองก็ยังอ่อน ๆ กับเรื่องพวกนี้ ยังไงขอ ตามอ่านละกันนะครับ

ปล.ขอบคุณเจ้าของกระทู้ที่ ตั้งคำถามนะครับ   Smiley
บันทึกการเข้า

waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« ตอบ #3 เมื่อ: 14 พฤษภาคม 2009, 02:36:28 »

ยังไงก็ช่วย ๆ กันนะครับ
มีเพื่อน ๆ หลายคนในบอร์ดไม่รู้เรื่องนี้แต่ก็ไม่กล้าถามครับ
ผมเลยเป็นตัวกลางในกลางตั้งกระทู้แทนให้ครับ Kiss
บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« ตอบ #4 เมื่อ: 14 พฤษภาคม 2009, 05:51:37 »

ดัน 1 ทีตอนเช้าครับ
บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« ตอบ #5 เมื่อ: 14 พฤษภาคม 2009, 08:09:43 »

ดันก่อนนอนเย็น ๆ ถ้ามีคนตอบจะรวบรวมเป็นสารบัญให้นะครับ :Smiley
บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
EThaiZone
เจ้าพ่อโลลิค่อน
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 321
ออฟไลน์ ออฟไลน์

กระทู้: 12,516



ดูรายละเอียด
« ตอบ #6 เมื่อ: 14 พฤษภาคม 2009, 11:17:34 »

ตอบอันแรก ขอแยกตามนี้ จากตัวที่เคยใช้

i (PCRE_CASELESS)
ตรวจแบบ case insensitive คือไม่สนพิมพ์ใหญ่พิมพ์เล็ก
m (PCRE_MULTILINE)
ยอมให้ตรวจหาแบบหลายบรรทัด ซึ่งปกติมันจะตรวจแค่บรรทัดเดียว
s (PCRE_DOTALL)
ทำให้ . หมายถึงทุกอักษร รวมถึงการขึ้นบรรทัดใหม่ด้วย
U (PCRE_UNGREEDY)
http://www.thaiseoboard.com/in...60804.msg745519.html#msg745519
u (PCRE_UTF8)
ให้ pattern รองรับการตรวจ utf8
บันทึกการเข้า

waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« ตอบ #7 เมื่อ: 14 พฤษภาคม 2009, 23:26:02 »

ดันไป ตอนนี้เทพกำลังออนไลน์เยอะ เผื่อฟลุ๊ค Grin
บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
Dr.K
Verified Seller
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 432
ออฟไลน์ ออฟไลน์

กระทู้: 6,744



ดูรายละเอียด เว็บไซต์
« ตอบ #8 เมื่อ: 14 พฤษภาคม 2009, 23:30:15 »

http://forums.doctornuke.com/a...-tips-regex-code-htaccess.html

http://board.thaihealth.net/showthread.php?p=1698#post1698

ตาคนเขียนคนเดียวกัน ใครไม่รุ
« แก้ไขครั้งสุดท้าย: 14 พฤษภาคม 2009, 23:32:40 โดย Dr.K » บันทึกการเข้า

waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« ตอบ #9 เมื่อ: 14 พฤษภาคม 2009, 23:32:51 »


ขอบคุณครับคุณหมอ Smiley
แต่ผมอ่านอังกฤษไม่ออกง่ะ Embarrassed
บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
Dr.K
Verified Seller
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 432
ออฟไลน์ ออฟไลน์

กระทู้: 6,744



ดูรายละเอียด เว็บไซต์
« ตอบ #10 เมื่อ: 14 พฤษภาคม 2009, 23:39:16 »

http://board.thaihealth.net/showthread.php?p=1698#post1698

 Tongue Lips Sealed จะบอกให้ว่าเขียนจนปวดไมเกรนเลยบางอัน
บันทึกการเข้า

Newbies
สมุนแก๊งเสียว
*

พลังน้ำใจ: 42
ออฟไลน์ ออฟไลน์

กระทู้: 554



ดูรายละเอียด
« ตอบ #11 เมื่อ: 15 พฤษภาคม 2009, 06:50:57 »

ขอดันด้วยครับ
แบบเคยใช้แต่ลอกๆ เขามา
แล้วก็ใช้ regular-expressions-cheat-sheet บ้าง ยามคิดอะไรไม่ออก




ไม่เข้าใจอีกอย่างคือ ตัวเปิดปิด pattern นี่เห็นหลายแบบ มันหมายถึงอะไร

'/pattern/'
'!pattern!'
'#pattern#'

บันทึกการเข้า



EThaiZone
เจ้าพ่อโลลิค่อน
เจ้าพ่อบอร์ดเสียว
*

พลังน้ำใจ: 321
ออฟไลน์ ออฟไลน์

กระทู้: 12,516



ดูรายละเอียด
« ตอบ #12 เมื่อ: 15 พฤษภาคม 2009, 08:29:57 »

ขอดันด้วยครับ
แบบเคยใช้แต่ลอกๆ เขามา
แล้วก็ใช้ regular-expressions-cheat-sheet บ้าง ยามคิดอะไรไม่ออก




ไม่เข้าใจอีกอย่างคือ ตัวเปิดปิด pattern นี่เห็นหลายแบบ มันหมายถึงอะไร

'/pattern/'
'!pattern!'
'#pattern#'




ต้องทำความเข้าใจแรกก่อนครับ คือ php มีการใช้ regex สองแบบ
POSIX (POSIX Extended) กับ PCRE (Perl-Compatible)

พวกคำสั่งตระกูล ereg จะเป็น posix
พวกคำสั่งตระกูล preg จะเป็น pcre

การใช้งานสองอันนี้จะต่างกันตรง pcre จะต้องมี delimiter คุมหน้าหลัง
เหมือนอย่างตัวอย่างสามอันที่คุณ Newbies ถามมา
พวก Delimiter ก็พวกตัว ' ! # นั่นแหละ
Delimiter จริงๆ ใช้สัญลักษณ์อะไรก็ได้ เพียงแต่ถ้ามันซ้ำใน pattern ก็ต้องใส่ \ นำหน้า

มีหลายคนชอบใส่แบบนี้
'/pattern/'

พอใช้งานจริงกับ html จะต้องเขียนแนวนี้
'/<a.*>pattern<\/a>/'

แต่ถ้าเราใช้ # เป็น delimiter แทน (อันนี้ผมใช้บ่อยมาก เพื่อกันความสับสนด้วย)
จะเขียนเป็นแบบนี้
'#<a.*>pattern</a>#'
บันทึกการเข้า

waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« ตอบ #13 เมื่อ: 15 พฤษภาคม 2009, 08:47:17 »

^
^
^
เทพมาอธิบายเอง Grin
บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
Tee++;
โปรแกรมเมอร์ จอหงวน
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 79
ออฟไลน์ ออฟไลน์

กระทู้: 1,861



ดูรายละเอียด เว็บไซต์
« ตอบ #14 เมื่อ: 18 พฤษภาคม 2009, 21:52:15 »

มันมีบอกใน manual แล้ว
อ้างถึง
The current possible PCRE modifiers are listed below. The names in parentheses refer to internal PCRE names for these modifiers. Spaces and newlines are ignored in modifiers, other characters cause error.


i (PCRE_CASELESS)
If this modifier is set, letters in the pattern match both upper and lower case letters.
m (PCRE_MULTILINE)
By default, PCRE treats the subject string as consisting of a single "line" of characters (even if it actually contains several newlines). The "start of line" metacharacter (^) matches only at the start of the string, while the "end of line" metacharacter ($) matches only at the end of the string, or before a terminating newline (unless D modifier is set). This is the same as Perl.

When this modifier is set, the "start of line" and "end of line" constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end. This is equivalent to Perl's /m modifier. If there are no "\n" characters in a subject string, or no occurrences of ^ or $ in a pattern, setting this modifier has no effect.

s (PCRE_DOTALL)
If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl's /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.
x (PCRE_EXTENDED)
If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include comments inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.
e (PREG_REPLACE_EVAL)
If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string. Single quotes, double quotes, backslashes and NULL chars will be escaped by backslashes in substituted backreferences.

Only preg_replace() uses this modifier; it is ignored by other PCRE functions.

Note: This modifier was not available in PHP 3.


A (PCRE_ANCHORED)
If this modifier is set, the pattern is forced to be "anchored", that is, it is constrained to match only at the start of the string which is being searched (the "subject string"). This effect can also be achieved by appropriate constructs in the pattern itself, which is the only way to do it in Perl.
D (PCRE_DOLLAR_ENDONLY)
If this modifier is set, a dollar metacharacter in the pattern matches only at the end of the subject string. Without this modifier, a dollar also matches immediately before the final character if it is a newline (but not before any other newlines). This modifier is ignored if m modifier is set. There is no equivalent to this modifier in Perl.
S
When a pattern is going to be used several times, it is worth spending more time analyzing it in order to speed up the time taken for matching. If this modifier is set, then this extra analysis is performed. At present, studying a pattern is useful only for non-anchored patterns that do not have a single fixed starting character.
U (PCRE_UNGREEDY)
This modifier inverts the "greediness" of the quantifiers so that they are not greedy by default, but become greedy if followed by "?". It is not compatible with Perl. It can also be set by a (?U) modifier setting within the pattern or by a question mark behind a quantifier (e.g. .*?).
X (PCRE_EXTRA)
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Any backslash in a pattern that is followed by a letter that has no special meaning causes an error, thus reserving these combinations for future expansion. By default, as in Perl, a backslash followed by a letter with no special meaning is treated as a literal. There are at present no other features controlled by this modifier.
J (PCRE_INFO_JCHANGED)
The (?J) internal option setting changes the local PCRE_DUPNAMES option. Allow duplicate names for subpatterns.
u (PCRE_UTF8)
This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.



อ้างถึง
PREG_PATTERN_ORDER Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on. This flag is only used with preg_match_all(). 
PREG_SET_ORDER Orders results so that $matches[0] is an array of first set of matches, $matches[1] is an array of second set of matches, and so on. This flag is only used with preg_match_all(). 
PREG_OFFSET_CAPTURE See the description of PREG_SPLIT_OFFSET_CAPTURE. This flag is available since PHP 4.3.0. 
PREG_SPLIT_NO_EMPTY This flag tells preg_split() to return only non-empty pieces. 
PREG_SPLIT_DELIM_CAPTURE This flag tells preg_split() to capture parenthesized expression in the delimiter pattern as well. This flag is available since PHP 4.0.5. 
PREG_SPLIT_OFFSET_CAPTURE If this flag is set, for every occurring match the appendant string offset will also be returned. Note that this changes the return values in an array where every element is an array consisting of the matched string at offset 0 and its string offset within subject at offset 1. This flag is available since PHP 4.3.0 and is only used for preg_split(). 
PREG_NO_ERROR Returned by preg_last_error() if there were no errors. Available since PHP 5.2.0. 
PREG_INTERNAL_ERROR Returned by preg_last_error() if there was an internal PCRE error. Available since PHP 5.2.0. 
PREG_BACKTRACK_LIMIT_ERROR Returned by preg_last_error() if backtrack limit was exhausted. Available since PHP 5.2.0. 
PREG_RECURSION_LIMIT_ERROR Returned by preg_last_error() if recursion limit was exhausted. Available since PHP 5.2.0. 
PREG_BAD_UTF8_ERROR Returned by preg_last_error() if the last error was caused by malformed UTF-8 data (only when running a regex in UTF-8 mode). Available since PHP 5.2.0. 
PCRE_VERSION PCRE version and release date (e.g. "7.0 18-Dec-2006"). Available since PHP 5.2.4. 
บันทึกการเข้า

waterandthenbrokenparting
คนรักเสียว
*

พลังน้ำใจ: 1
ออฟไลน์ ออฟไลน์

กระทู้: 111



ดูรายละเอียด
« ตอบ #15 เมื่อ: 18 พฤษภาคม 2009, 21:56:49 »

^
^
^
^
^
ปัญหาคืออ่านไม่ออกนี่หล่ะครับ Grin
บันทึกการเข้า

รักแท้แพ้ใกล้ชิด
หน้า: [1]   ขึ้นบน
พิมพ์