Jump to content

regex help


superaktieboy

Recommended Posts

hi

im trying to make a template parser in php.. am a bit stuck tho..

im trying to parse the following: {blockname.blockname.varname}

that seems to work fine if there is only one of those. but if i have mutliple vars next to eachother like this: {blockname.blockname.varname1} {blockname.blockname.varname2}

the same thing doesn't work..

my regex currently is:

/(\{([^\}](.*?\.)+(.*?)+)\})/

 

and im using preg_match to find them. however, it doesn't seem to work, i keep getting the following as the result:

Array
(
    [0] => {blockname.blockname.varname1} {blockname.blockname.varname2}
    [1] => {blockname.blockname.varname1} {blockname.blockname.varname2}
    [2] => blockname.blockname.varname1} {blockname.blockname.varname2
    [3] => blockname.
    [4] => 
)

 

no i use the third array argument ([2]).. which is supposed to be blockname.blockname.varname1 (without the brackets).. but if i have multiple vars on the same line, i get all the stuff inbetween the first and the last var. as u can see i have also tried adding [^\}] but it doesn't seem to work either..

how can i get multiple of these vars (without the brackets if possible) if they're on the same line? coz what i put down for the regex seems to fail epicly.

 

thanks for your help :)

super...

 

Link to comment
Share on other sites

If I understand correctly, would this be along the lines of what you are looking for?

 

$str = 'Some garbge...{blockname.blockname.varname1}...more garbage...{blockname.blockname.varname2}.. and more garbge yet...{blockname.blockname.varname3}';
preg_match_all('#{(?:[a-z0-9]+\.?)+}#i', $str, $matches);
echo "<pre>".print_r($matches[0], true);

 

output:

Array
(
    [0] => {blockname.blockname.varname1}
    [1] => {blockname.blockname.varname2}
    [2] => {blockname.blockname.varname3}
)

 

this of course makes assumptions like the characters between { and the dots and } are a-z (case insensitive) or 0-9. It's a quick and dirty method which can be made more precise, but does the job for the examples you provided. Is this what you're looking for?

Link to comment
Share on other sites

are those the only things on the line?

nope not necessary

 

Can you give actual examples of the context these things are in?  I'm thinking it would be easier to just

 

~\{([^\}]+)\}~

 

and then explode at the dot.

hmm sounds like an idea.. im gonna see if nrg_alpha's method works.. if it doesn't ill be using this.. thanks

 

If I understand correctly, would this be along the lines of what you are looking for?

 

$str = 'Some garbge...{blockname.blockname.varname1}...more garbage...{blockname.blockname.varname2}.. and more garbge yet...{blockname.blockname.varname3}';
preg_match_all('#{(?:[a-z0-9]+\.?)+}#i', $str, $matches);
echo "<pre>".print_r($matches[0], true);

 

output:

Array
(
    [0] => {blockname.blockname.varname1}
    [1] => {blockname.blockname.varname2}
    [2] => {blockname.blockname.varname3}
)

 

this of course makes assumptions like the characters between { and the dots and } are a-z (case insensitive) or 0-9. It's a quick and dirty method which can be made more precise, but does the job for the examples you provided. Is this what you're looking for?

yeah i think that is it :) thanks :)

Link to comment
Share on other sites

True enough. If the goal is just to get what's between { and }, your solution is fine (had I understood it that way, I would have proposed almost the same thing - see message below). I was under the assumption of a specific format..(I am seeing the examples provided, and I start thinking { something . something . somethingx } and forgot about the no brackets thing..

 

In your sample, you don't need to escape the { and } characters.. you could simply do this:

 

~{([^}]+)}~

 

as these curly braces are already understood / treated as literals.

Link to comment
Share on other sites

superaktieboy, CV's method works if all you want is what is between { and }. If there are specifics that need to be met within those braces, then you should specify (unless you example is about as specific as needed.. even then, my solution would still allow for something like:

 

{blockname1.blockname1.varname1}

 

I was admittedly making it somewhat open ended (not as much as CV's granted). So if there is a specific format, let us know.

Link to comment
Share on other sites

superaktieboy, CV's method works if all you want is what is between { and }. If there are specifics that need to be met within those braces, then you should specify (unless you example is about as specific as needed.. even then, my solution would still allow for something like:

 

{blockname1.blockname1.varname1}

 

I was admittedly making it somewhat open ended (not as much as CV's granted). So if there is a specific format, let us know.

yeah i know.. and decided to use CV's method because it picks up more, you never know i might need more than just a-z and 0-9.. thanks to you both anyway :)

Link to comment
Share on other sites

True enough. If the goal is just to get what's between { and }, your solution is fine (had I understood it that way, I would have proposed almost the same thing - see message below). I was under the assumption of a specific format..(I am seeing the examples provided, and I start thinking { something . something . somethingx } and forgot about the no brackets thing..

 

In your sample, you don't need to escape the { and } characters.. you could simply do this:

 

~{([^}]+)}~

 

as these curly braces are already understood / treated as literals.

 

You do have to escape them...{ and } are special chars in the regex.  For instance,

 

~^\w{5,10}$~  // alphanumeric string 5 to 10 chars long

 

edit: well, maybe not inside the neg char class, but outside, yes.

Link to comment
Share on other sites

When you use it as a quantity, as what you have illustrated with \w{5,10} obviously you don't escape them... but outside of that, you still don't need to.

By example, If I had to escape { and }, I would not get those curly braces in this example:

 

$str = '{123}';
preg_match('#{\d+}#', $str, $match);
// $match[0] = {123}

 

Of course, you can escape them if you want to (and still get the same results). But my point is you don't need to.

So in your previous example ~\{([^\}]+)\}~, you have escaped them when there was no need to, as the results are the exact same without escaping those curly braces.

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.