Does it work?

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Does it work?

Post by visonardo »

i want to keep an ocurrence but here must be not ones determinated datas. for example, if i wanna capture a word that must be not following by magazine word it would be thus

Code: Select all

preg_match_all("word\s*(?!magazine)",$source,$matchs);
but if its not a specific word? but yes i have the conditions of determinated word.

Code: Select all

$condition="regex code here";

preg_match_all("word\s*(?!".$condition.")",$source,$matchs);

can i do it and work?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have you tried it?
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

this

Code: Select all

preg_match_all("((7\d)|(a\W))",$a,$matchs);

should be the same that

Code: Select all

preg_match_all("((7\d)|(a(?!\w)))",$a,$matchs);

but the last didnt work :?




how you would do (?!regex_code_here)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why would it be the same? The first is using basic patterns and metacharacters. The second is using a negative forward assertion. Quite different.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

feyd wrote:Why would it be the same? The first is using basic patterns and metacharacters. The second is using a negative forward assertion. Quite different.

the result is the same to

(?!a) than [^a] ----> Do im mistaken?

but would like that be thus to

(?!\w) than [^\w] and than \W but i guess that not.


Does someone tried that?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

visonardo wrote:Does someone tried that?
Why can't you try it?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Negative lookahead versus negated character class

Post by GeertDD »

visonardo wrote:the result is the same to

(?!a) than [^a] ----> Do im mistaken?
A negative lookahead is not exactly the same as a negated character class. The main difference (as far as I know) is that a negated character class still needs to match a character, a character not listed in the class. A negative lookahead doesn't match (or 'consume') a character -- it only matches a position in the string.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Negative lookahead versus negated character class

Post by Chris Corbyn »

GeertDD wrote:
visonardo wrote:the result is the same to

(?!a) than [^a] ----> Do im mistaken?
A negative lookahead is not exactly the same as a negated character class. The main difference (as far as I know) is that a negated character class still needs to match a character, a character not listed in the class. A negative lookahead doesn't match (or 'consume') a character -- it only matches a position in the string.
Correct :) Lookaheads and lookbehinds are not actually consuming characters, they are just looking for the presence of them.
Post Reply