Forums

Forums (http://www.abandonia.com/vbullet/index.php)
-   Programming (http://www.abandonia.com/vbullet/forumdisplay.php?f=25)
-   -   Regular Expressions (http://www.abandonia.com/vbullet/showthread.php?t=8946)

Kon-Tiki 26-01-2006 09:32 AM

I'm learning Regex now, and'm quite stumped. I'd ask the instructor, but he's said to be no good with them either yesterday. I've made a test-page to get the hang of it, but its output's not what it should do, according to how I read the code.

Code:

*$string = "Best niet [b] bevet Moddervet Niet vet";
 *echo $string, "<br>";

 *eregi("(\[b\])", $string, $resultaat);
 *$open_tags = count($resultaat);

 *eregi("(\[/b\])", $string, $resultaat_closed);
 *$closed_tags = count($resultaat_closed);

 *echo "Open: ", $open_tags, "<br>Closed: ", $closed_tags;

That's my code (stripped of all set-up tags like <html> etc). This's its output:
Code:

Best niet [b] bevet Moddervet Niet vet
Open: 2
Closed: 0

No matter how many results it should find, it'll always give 2, except if there're no results, like in Closed. Then it'll give 0. I don't see why it doesn't give 1 for the Open one in the current example. Any help'd be greatly appreciated :)

Rogue 26-01-2006 09:59 AM

I did work a bit with regular expressions, but not in PHP.

Can you first explain what do you like program to do?

Kon-Tiki 26-01-2006 10:32 AM

I would like the program to tell me how many times it encounters [*b*] and how many times it encounters [/*b*] (without the *s) in a certain string. It then should add as many [/*b*]s as needed if the amount of [*b*]s is bigger than the amount of [/*b*]s.

In the end, it should turn all the [*b*][/*b*]-tags to HTML's [b] tags, but it should close as many as are opened, or it'd mess up my input. I eventually'd like to do that with hyperlinks too, which makes it so complicated.

Bobbin Threadbare 26-01-2006 10:59 AM

This is PHP? :blink:

Kon-Tiki 26-01-2006 11:23 AM

Yep, regular expressions within PHP. It's one of the biggest brainwreckers out there for it, but sometimes a necessity. For more basic information, you can always look here. The site explains what it is, but it won't help me solve problem. Preg_match_all() can, though. Am going to try that one out now.

Data 26-01-2006 11:29 AM

uhmn you misunderstood what is placed in $resultaat

in $resultaat[0] the copy of the total matched string is placed
in $resultaat[1] is the match for the first () sequence
in $resultaat[2] is the match for the second () sequence stored.

so if you match for [*b*] then you will find in 0 [*b*] (as it's the full match) and in 1 (as it the first set of () that is matched). As you don't match for a second pair of () you will find any more matches.
The count idea you are trying to implement will not work this way.

Kon-Tiki 26-01-2006 11:42 AM

Var_dump() gives wrong results too.
Code:

$string = "kakakapipikaka";
$reg_ex = "(ka)";
eregi($reg_ex, $string, $test);
echo "Test: ", var_dump($test), "<br>";

Output:
Code:

Test: array(2) { [0]=> *string(2) "ka" [1]=> *string(2) "ka" }
The way that should be, would be [0]=> string(2) "ka" [1]=> string(2) "ka" [2]=> string(2) "ka" [3]=> string(2) "ka" [4]=> string(2) "ka" [5]=> string(2) "ka" (with something different for [0]), if I understand correctly.

A different solution works now, though.
$number = preg_match_all($reg_ex, $string, $resultaat) returns 5 :)

Data 26-01-2006 11:52 AM

well the example you posted using var_dump gives the correct output for
ereg(i)

you don't understand what eregi matches. (it only matches the string ka once.)
and as that is the total string as well you see it in both 0 and 1


Kon-Tiki 26-01-2006 11:56 AM

So eregi() stops the moment it finds a match. That's pretty useful, but not for what I'm trying to do :D Thanks for the clarification :cheers:

Data 26-01-2006 12:17 PM

well it can match it more often if you specify it in the regexstring.
but it's not suitable for counting.
the preg_match_all seems a better choice for that


The current time is 11:56 PM (GMT)

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.