Page 1 of 1

keep the ocurrence

Posted: Tue Oct 17, 2006 7:50 pm
by visonardo
i have this regex

(\"|')?(http|ftp)(s)?: ((\w+)\.)?(\w)+\.(com|net)(\.ar)?(\"|')?

the task is that must appear a url between ' or " but not with ' and " in the end, or viceversa. or if appear in the start exactly it must appear in the end. understand what i want? how is it?

Posted: Wed Oct 18, 2006 4:05 am
by jmut

Code: Select all

$string = "'dfd\"";
$pattern = '#("|\')(.*?)\1#';


preg_match($pattern,$string,$match);
var_export($match);

OUTPUT
array (
)


$string = "'dfd'";
preg_match($pattern,$string,$match);
var_export($match);

OUTPUT:
array (
  0 => '\'dfd\'',
  1 => '\'',
  2 => 'dfd',
)

Posted: Wed Oct 18, 2006 1:06 pm
by visonardo
Thank :D

but --> [^\1#]

and something like that? how is it? (i tested thus [^\1#] but didnt work)

idont understand how is to able almost anything but not a determinated word, here in some part someone asked that but was not so cleared the example that given like their reply. [^a] dont allow a letter, but i would like something thus [^(word1,word2,word3)] and it would not allow the words "word1", "word2" and "word3"

how it would be? :roll:

Posted: Wed Oct 18, 2006 1:13 pm
by feyd
[] are a character class construct. The letters/numbers/what-have-you inside them are allowed to happen arbitrary to their order.

Matching entires words (or rather not matching words) involves a different construct called assertions.

Code: Select all

(?=word1|word2|word3)
Is a (positive forward) assertion.

Code: Select all

(?!word1|word2|word3)
is a negative (forward) assertion.

Posted: Wed Oct 18, 2006 7:39 pm
by visonardo
thank you :D

but what happened here?

Code: Select all

<?

$b=' abc def ';
$fun2='(?=abc)';

preg_match_all("/".$fun2."/i",$b,$match);

print_r($match);
?>
output

Code: Select all

Array
(
    [0] => Array
        (
            [0] => 
        )

)
matchs null? 8O 8O 8O

How i must use that?

Posted: Wed Oct 18, 2006 9:02 pm
by feyd
They are zero width constructs, they only signal whether the rest of the pattern is processed really. If you want to capture the text, remove the ?=

Posted: Thu Oct 26, 2006 10:43 am
by visonardo
other thing. This is my code:

Code: Select all

<?

function ech($aver)
{
	print_r($aver);
	echo '<p>';
	return;
}



$a=" something avascr:function1('','http:www.google.com/logo2.gif'); kjlskjfñal  ñklasjfñlakj jlafñs here was ---> javascript:function2('','http:www.google.com/logo.gif'); <--- here was";

$fun="(?!javascript):[\w]+\([\S]*(('|\")((http|ftp)s?:(.*))\\2)[\S]*\);";

preg_replace_callback("/".$fun."/i","ech",$a);
?>

when capture the first element the [0] is all it: ":function1('','http:www.google.com/logo2.gif'); kjlskjfñal ñklasjfñlakj jlafñs here was ---> javascript:function2('','http:www.google.com/logo.gif');"

(without double quotes in the beginning neither in the start, of course)

i print me:

Code: Select all

Array
(
    [0] => :function1('','http:www.google.com/logo2.gif'); kjlskjfñal  ñklasjfñlakj jlafñs here was ---> javascript:function2('','http:www.google.com/logo.gif');
    [1] => 'http:www.google.com/logo2.gif'); kjlskjfñal  ñklasjfñlakj jlafñs here was ---> javascript:function2('','http:www.google.com/logo.gif'
    [2] => '
    [3] => http:www.google.com/logo2.gif'); kjlskjfñal  ñklasjfñlakj jlafñs here was ---> javascript:function2('','http:www.google.com/logo.gif
    [4] => http
    [5] => www.google.com/logo2.gif'); kjlskjfñal  ñklasjfñlakj jlafñs here was ---> javascript:function2('','http:www.google.com/logo.gif
)
<p>

if you see, it should capture :function1(...); and no more, but then of the ; it continue :s, why? if i said that ; is in the end. i tested puting [^;]* and not .* and it fix the problem, but i guess that i would not be needed, it should take until the first ; why happen that?

Posted: Thu Oct 26, 2006 11:07 am
by visonardo
well, i changed (.*) by ([^;]*) in a part the problem has been fixed but the part (?!javascript) dont work because take too the functions that dont start with javascript and it should be not.

the last result (with the change that i told) is:

Code: Select all

Array
(
    [0] => :function1('','http:www.google.com/logo2.gif')
    [1] => 'http:www.google.com/logo2.gif'
    [2] => '
    [3] => http:www.google.com/logo2.gif
    [4] => http
    [5] => www.google.com/logo2.gif
)
<p>Array
(
    [0] => :function2('','http:www.google.com/logo.gif')
    [1] => 'http:www.google.com/logo.gif'
    [2] => '
    [3] => http:www.google.com/logo.gif
    [4] => http
    [5] => www.google.com/logo.gif
)
<p>

just would take one and not two, what is bad here?