keep the ocurrence

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.

keep the ocurrence

Post 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?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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',
)
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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 ?=
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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?
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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?
Post Reply