PDA

View Full Version : lookarounds در عبارات با قاعده



wolf_majid
شنبه 05 مرداد 1392, 18:09 عصر
سلام دوستان
میشه چند عبارت زیر را برام توضیح بدید(لطفا" با مثال)

(?=exp)
Match any position preceding a suffix exp

(?<=exp)
Match any position following a prefix exp

(?!exp)
Match any position after which the suffix exp is not found

(?<!exp)

Match any position before which the prefix exp is not found

wolf_majid
یک شنبه 06 مرداد 1392, 07:39 صبح
لااقل بگید منظور از عبارت پسوندی و پیشوندی در اینجا چیه ؟

hercool
یک شنبه 06 مرداد 1392, 08:24 صبح
پسوندی یعنی عملگر ها بعد از عملوند ها قرار میگیره مثلا (x+y) اینجوری میشه +xy
پیشوندی یعنی عملگر ها قبل از عملوند ها قرار میگیره برای مثال بالا xy+
یه سرچ کوچولو بهت کمک بیشتری میکنه در حوزه درس ساختمان داده هست

wolf_majid
یک شنبه 06 مرداد 1392, 08:26 صبح
فکر نمیکنم در عبارات باقاعده منظور توضیح شما بوده باشه

hercool
یک شنبه 06 مرداد 1392, 08:32 صبح
چون شما گفتی پسوندی و پیشوندی چیه من این بخش رو می دونستم در اختیارتون گذاشتم
exp که به معنی عبارت هست
? در عبارات با قاعده به معنی برای هیچ یا یک کارکتر هست یعنی مثلا برای عبارت x? به معنی این هست که جای ? می تونه هیچی نباشه یا می تونه یک کارکتر باشه (نه بیشتر)
اما اگر جاش * باشه به معنی اینه که برای هیچ یا چندین کارکتر هست

wolf_majid
دوشنبه 07 مرداد 1392, 22:05 عصر
دوستان اگر کسی میدونه ممنون میشم توضیح بده

tooraj_azizi_1035
دوشنبه 07 مرداد 1392, 22:11 عصر
Positive Lookaround

The next four are so-called lookahead or lookbehind assertions. They look for things that go before or after the current match without including them in the match. It is important to understand that these expressions match a position like "^" or "\b" and never match any text. For this reason, they are known as "zero-width assertions". They are best illustrated by example:

"(?=exp)" is the "zero-width positive lookahead assertion". It matches a position in the text that precedes a given suffix, but doesn't include the suffix in the match:

22. \b\w+(?=ing\b) The beginning of words ending with "ing"

"(?<=exp)" is the "zero-width positive lookbehind assertion". It matches the position following a prefix, but doesn't include the prefix in the match:

23. (?<=\bre)\w+\b The end of words starting with "re"

Here is an example that could be used repeatedly to insert commas into numbers in groups of three digits:

24. (?<=\d)\d{3}\b Three digits at the end of a word, preceded by a digit

Here is an example that looks for both a prefix and a suffix:

25. (?<=\s)\w+(?=\s) Alphanumeric strings bounded by whitespace



منبع: http://www.codeproject.com/Articles/9099/The-30-Minute-Regex-Tutorial

wolf_majid
دوشنبه 07 مرداد 1392, 22:21 عصر
ممنون آقا تورج
ولی همین منبع رو خوندم و نفهمیدم :افسرده:
مثلا" در مثال 22 گفته
شروع کلمات با ing ویا i,n,g ختم بشه در حالیکه نرم افزار expresso خروجی های دیگه ای میده

tooraj_azizi_1035
سه شنبه 08 مرداد 1392, 14:43 عصر
عبارت باقاعده ای مثل (?=exp) اصطلاحاً zero-width هست یعنی خودش Match نمیشه بلکه به یک نقطه اشاره میکنه.

مثال: work(?=ing)

باعث انتخاب work در این جمله میشه:
he is working...

برای اینکه بگید working حتماً باید به طور کلی Match بشه باید اون رو به موتور Regex بفهمونید:
\bwork(?=ing\b)

\b بعد از ing و قبل از work کلی بودن یا تنها پیدا کردن working رو نشون میده. (هر دو لازمه) همون Match whole words در Find رو معنی میده.


و برای اینکه خود ing هم شامل انتخاب بشه:

\bwork(?=ing)\w+

نتیجه اینکه عبارت باقاعده مثال 22 مانند ^ یا $ به یک مکان اشاره می کنند و خودشان انتخاب نمی شوند.



اینجا تست کن:http://regexpal.com/