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 :)

Link to comment
Share on other sites

/(?:[^\\]|^)\[[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

Link to comment
Share on other sites

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

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.