BeBot - An Anarchy Online and Age Of Conan chat automaton

Development => Coding and development discussion => Topic started by: lazarus on November 01, 2007, 10:01:43 pm

Title: input message parser (regex)
Post by: lazarus on November 01, 2007, 10:01:43 pm
Hey all,

Im trying to make a regular expressions that can handle all the bot commands.
well maybe this is not needed but, how do I do it otherwise?

I got it to grab multiply items from one command.

the code look like this:
Code: [Select]
<?php

/**
 *
 *
 * @version $Id$
 * @copyright 2007
 */
$input 'loot add <a href="itemref://206204/206204/1">Twilight\'s Murder</a> <a href="itemref://223322/206204/1">I am gimp</a> test';
preg_match_all('/<a href="([^"]*)">([^<]*)<\/a>/im'$input$matchesPREG_SET_ORDER);
    
print_r($matches);
?>


and it result in this:
Code: [Select]
Array
(
    [0] => Array
        (
            [0] => <a href="itemref://206204/206204/1">Twilight's Murder</a>
            [1] => itemref://206204/206204/1
            [2] => Twilight's Murder
        )

    [1] => Array
        (
            [0] => <a href="itemref://223322/206204/1">I am gimp</a>
            [1] => itemref://223322/206204/1
            [2] => I am gimp
        )

)

but I still lack to be able to grab loot add and test

I tryed to put in (\w*) thro it give all kind of diffrent output.

The output im looking for is this:
Code: [Select]
Array
(
    [0] => Array
       (
            [0] => loot
       )
    [1] => Array
       (
            [0] => add
       )
    [2] => Array
       (
            [0] => <a href="itemref://206204/206204/1">Twilight's Murder</a>
            [1] => itemref://206204/206204/1
            [2] => Twilight's Murder
        )

    [3] => Array
        (
            [0] => <a href="itemref://223322/206204/1">I am gimp</a>
            [1] => itemref://223322/206204/1
            [2] => I am gimp
        )
    [4] => Array
       (
            [0] => test
       )
)

this parser should be working with all count of pre word/numbers and after word/numbers :)
Title: Re: input message parser (regex)
Post by: Blueeagle on November 01, 2007, 10:12:38 pm
I do not understand the question.
Title: Re: input message parser (regex)
Post by: Alreadythere on November 01, 2007, 11:16:02 pm
The regular expression returns exactly what it's supposed to. Maybe you'll have to use more then one regular expression, or a more general one, or split up the input before preg_match(). I doubt that it's possible to match your wished output format exactly though.
Title: Re: input message parser (regex)
Post by: lazarus on November 02, 2007, 10:15:18 am
The regular expression returns exactly what it's supposed to. Maybe you'll have to use more then one regular expression, or a more general one, or split up the input before preg_match(). I doubt that it's possible to match your wished output format exactly though.

ok how do I split it up?

I just want to be able to check against the words and items.

Title: Re: input message parser (regex)
Post by: lazarus on November 02, 2007, 10:20:11 am
I do not understand the question.

I want to put the input string
Code: [Select]
"loot add <a href="itemref://206204/206204/1">Twilight\'s Murder</a> <a href="itemref://223322/206204/1">I am gimp</a> test"
into a array with each section in it.
as in

loot, add, first item, 2nd item, test.

So I can if ($output[4] == "test") echo "this is what I want";
and so on :)
Title: Re: input message parser (regex)
Post by: lazarus on November 02, 2007, 05:01:29 pm
I found the solution that I can use.

Code: [Select]
<pre>
<?php

/**
 *
 *
 * @version $Id$
 * @copyright 2007
 */

$str 'Søg på <a href="http://www.google.com/">google</a> <a href="http://www.google.com/">google</a> for at finde det du mangler';
$chars preg_split('/(<a.*<\/a>)/im'$str, -1PREG_SPLIT_DELIM_CAPTURE);
print_r($chars);
?>

</pre>

that give this result :) :
Code: [Select]
<pre>
Array
(
    [0] => Søg på
    [1] => <a href="http://www.google.com/">google</a> <a href="http://www.google.com/">google</a>
    [2] =>  for at finde det du mangler
)
</pre>

from this I know that chars[0] is all before the items, chars[1] is all items posted and chars[2] is everything after items
SimplePortal 2.3.7 © 2008-2025, SimplePortal