اگر مشخصات کلاس Pattern را مرور کنید، جداولی خلاصهکننده از ساختارهای عبارت باقاعدهی پشتیبانیشده میبینید. در این بخش موارد زیر را خواهید یافت:
ستون سمت چپ ساختارهای عبارت باقاعده و ستون سمت راست شرایط تطبیق هر ساختار را توصیف میکند.
کلمهی class در این جدول به معنای کلاس کاراکتری (Character Class) است. بهعنوان مثال [abc] یک کلاسی ساده است. این کلاسها هیچ ارتباطی با کلاسهای جاوا که کد در آنها مینویسید ندارند. در زمینهی عبارات باقاعده، یک کلاس کاراکتری مجموعهای از کاراکترها داخل براکتهای مربعی است و کاراکترهایی را که با یک کاراکتر از رشتهی ورودی تطبیق میدهند مشخص میکند.
| ساختار | توضیحات |
|---|---|
[abc] | a، b یا c (کلاس ساده) |
[^abc] | هر کاراکتری غیر از a، b یا c (نقیض) |
[a-zA-Z] | a تا z، یا A تا Z، شامل هر دو انتها (بازه) |
[a-d[m-p]] | a تا d، یا m تا p: [a-dm-p] (اجتماع) |
[a-z&&[def]] | d، e یا f (تقاطع) |
[a-z&&[^bc]] | a تا z، به جز b و c: [ad-z] (تفریق) |
[a-z&&[^m-p]] | a تا z، و نه m تا p: [a-lq-z] (تفریق) |
سادهترین شکل کلاس کاراکتری قرار دادن مجموعهای از کاراکترها در کنار هم در براکتهای مربعی است. بهعنوان مثال عبارت باقاعده [bcr]at کلمات «bat»، «cat» یا «rat» را تطبیق میدهد چون یک کلاس کاراکتری (پذیرش «b»، «c» یا «r») بهعنوان کاراکتر اول، و سپس دو حرف a و t را تعریف میکند.
مثال زیر را امتحان کنید:
Enter your regex: [bcr]at
Enter input string to search: bat
I found the text "bat" starting at index 0 and ending at index 3.
Enter your regex: [bcr]at
Enter input string to search: cat
I found the text "cat" starting at index 0 and ending at index 3.
Enter your regex: [bcr]at
Enter input string to search: rat
I found the text "rat" starting at index 0 and ending at index 3.
Enter your regex: [bcr]at
Enter input string to search: hat
No match found.
در مثالهای بالا، تطبیق کلی فقط زمانی موفق است که حرف اول با یکی از کاراکترهای تعریفشده توسط کلاس کاراکتری مطابقت داشته باشد.
برای تطبیق تمام کاراکترها به جز آنهایی که فهرست شدهاند، متاکاراکتر «^» را در ابتدای کلاس کاراکتری قرار دهید. این تکنیک بهعنوان Negation شناخته میشود.
Enter your regex: [^bcr]at
Enter input string to search: bat
No match found.
Enter your regex: [^bcr]at
Enter input string to search: cat
No match found.
Enter your regex: [^bcr]at
Enter input string to search: rat
No match found.
Enter your regex: [^bcr]at
Enter input string to search: hat
I found the text "hat" starting at index 0 and ending at index 3.
تطبیق فقط زمانی موفق است که کاراکتر اول رشتهی ورودی شامل هیچیک از کاراکترهای تعریفشده توسط کلاس کاراکتری نباشد.
گاهی میخواهید کلاس کاراکتریای تعریف کنید که شامل بازهای از مقادیر باشد، مانند حروف «a تا h» یا اعداد «1 تا 5». برای مشخص کردن بازه، متاکاراکتر «-» را بین اولین و آخرین کاراکتر مورد تطبیق قرار دهید، مانند [1-5] یا [a-h]. همچنین میتوانید بازههای مختلف را در کنار هم در کلاس قرار دهید. بهعنوان مثال [a-zA-Z] هر حرف الفبا را تطبیق میدهد: a تا z (کوچک) یا A تا Z (بزرگ).
چند مثال از بازهها و Negation:
Enter your regex: [a-c]
Enter input string to search: a
I found the text "a" starting at index 0 and ending at index 1.
Enter your regex: [a-c]
Enter input string to search: b
I found the text "b" starting at index 0 and ending at index 1.
Enter your regex: [a-c]
Enter input string to search: c
I found the text "c" starting at index 0 and ending at index 1.
Enter your regex: [a-c]
Enter input string to search: d
No match found.
Enter your regex: foo[1-5]
Enter input string to search: foo1
I found the text "foo1" starting at index 0 and ending at index 4.
Enter your regex: foo[1-5]
Enter input string to search: foo5
I found the text "foo5" starting at index 0 and ending at index 4.
Enter your regex: foo[1-5]
Enter input string to search: foo6
No match found.
Enter your regex: foo[^1-5]
Enter input string to search: foo1
No match found.
Enter your regex: foo[^1-5]
Enter input string to search: foo6
I found the text "foo6" starting at index 0 and ending at index 4.
همچنین میتوانید از اجتماع برای ساخت یک کلاس کاراکتری واحد از دو یا چند کلاس کاراکتری جداگانه استفاده کنید. یک کلاس را داخل کلاس دیگر تودرتو کنید، مانند [0-4[6-8]]. این اجتماع خاص، یک کلاس کاراکتری واحد ایجاد میکند که اعداد ۰، ۱، ۲، ۳، ۴، ۶، ۷ و ۸ را تطبیق میدهد.
Enter your regex: [0-4[6-8]]
Enter input string to search: 0
I found the text "0" starting at index 0 and ending at index 1.
Enter your regex: [0-4[6-8]]
Enter input string to search: 5
No match found.
Enter your regex: [0-4[6-8]]
Enter input string to search: 6
I found the text "6" starting at index 0 and ending at index 1.
Enter your regex: [0-4[6-8]]
Enter input string to search: 8
I found the text "8" starting at index 0 and ending at index 1.
Enter your regex: [0-4[6-8]]
Enter input string to search: 9
No match found.
برای ساخت یک کلاس کاراکتری واحد که فقط کاراکترهای مشترک بین تمام کلاسهای تو در تو را تطبیق دهد، از && استفاده کنید، مانند [0-5&&[3-9]]. این تقاطع خاص، کلاسی واحد میسازد که فقط اعداد مشترک بین هر دو کلاس کاراکتری: ۳، ۴ و ۵ را تطبیق میدهد.
Enter your regex: [0-9&&[345]]
Enter input string to search: 3
I found the text "3" starting at index 0 and ending at index 1.
Enter your regex: [0-9&&[345]]
Enter input string to search: 4
I found the text "4" starting at index 0 and ending at index 1.
Enter your regex: [0-9&&[345]]
Enter input string to search: 5
I found the text "5" starting at index 0 and ending at index 1.
Enter your regex: [0-9&&[345]]
Enter input string to search: 2
No match found.
Enter your regex: [0-9&&[345]]
Enter input string to search: 6
No match found.
و مثالی از تقاطع دو بازه:
Enter your regex: [2-8&&[4-6]]
Enter input string to search: 3
No match found.
Enter your regex: [2-8&&[4-6]]
Enter input string to search: 4
I found the text "4" starting at index 0 and ending at index 1.
Enter your regex: [2-8&&[4-6]]
Enter input string to search: 5
I found the text "5" starting at index 0 and ending at index 1.
Enter your regex: [2-8&&[4-6]]
Enter input string to search: 6
I found the text "6" starting at index 0 and ending at index 1.
Enter your regex: [2-8&&[4-6]]
Enter input string to search: 7
No match found.
در نهایت میتوانید از تفریق برای Negate کردن یک یا چند کلاس کاراکتری تو در تو استفاده کنید، مانند [0-9&&[^345]]. این مثال کلاسی واحد ایجاد میکند که همه چیز از ۰ تا ۹ را به جز اعداد ۳، ۴ و ۵ تطبیق میدهد.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 2
I found the text "2" starting at index 0 and ending at index 1.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 3
No match found.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 4
No match found.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 5
No match found.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 6
I found the text "6" starting at index 0 and ending at index 1.
Enter your regex: [0-9&&[^345]]
Enter input string to search: 9
I found the text "9" starting at index 0 and ending at index 1.
حالا که نحو ساخت کلاسهای کاراکتری را یاد گرفتید، ممکن است بخواهید قبل از ادامه جدول کلاسهای کاراکتری را مرور کنید.
این محتوا کاملا رایگان توسط تیم کدلپر ترجمه شده و در اختیار شما کاربران عزیز قرار گرفته است، هر گونه کپی برداری برای مقاصد غیر رایگان و بدون ذکر منبع، مورد پیگیری قانونی قرار میگیرد.
ترجمه شده از منبع: https://dev.java/learn/