Jump to content

i've got an expression :) just needs...correcting


GoneNowBye

Recommended Posts

i'm at school now on my phone btw, so if its obvouse its because my mind is focused on the pending stats exam

 

heres the problem

 

i need a REGEX expression which matches "[this] but not \[this]"

my expression : /[^\\]\[[(a-z)]*\]/  its fine, but it wont match correctly if a valid tag ([tag]) is at the start of the string

"[hello] \[world]" no matches

" [hello] \[world]" matches [hello]

please help :)

/(?:[^\\]|^)\[[a-z]*\]/

 

works!!!

 

but it returns a multidmensional array, i want the [1] one...can i get rid of the first (memory is a concern serously)

 

 

Array
(
    [0] => Array
        (
            [0] => [hello]
            [1] =>  [test]
        )

    [1] => Array
        (
            [0] => [hello]
            [1] => [test]
        )

)

 

 

the first one has a trailing space

The issue has nothing to do with your expression. It has to do with how the PHP function returns matches. I assume you are using preg_match_all(): http://us.php.net/manual/en/function.preg-match-all.php

 

Please read the documentation on how values are populated into the $matches parameter and the available flags to modify the returned values. The default is to

  Quote
Orders results so that $matches[0] is an array of full pattern matches, $matches[1] is an array of strings matched by the first parenthesized subpattern, and so on

 

I don't believe you can get what you are after, but you can try experimenting with the other flags.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.