Jump to content

regex for matching any number of chars in string


dsaba

Recommended Posts

I know

[a-z]{3} will match:

'hey'

 

I want to say [a-z]{any number of chars}

 

the reason is i'm parsing through a php file and want to match all functions

 

here's a sample string

 

function funcName(list of args) {

code body

}

 

 

any ideas with this?

-thanks

 

 

this doesnt quite work:

/[fF]unction (.*) \((.*)\) \{(.*)\}/

Link to comment
Share on other sites

I'm not really sure what you mean, but to match any number of characters in a given character range you use *:

 

preg_match("/^([a-z]*)$/",$string)

 

The above regex will match 'asdfaslekehe', 'a', '' ect. Any number of characters as you said.

Link to comment
Share on other sites

well... I dont know how to put this without sound as if I want you to write it for me.. :)

 

 

I want to match any number of the following patterns in a php file

function funcName(list of arguments) {

code body

}

 

look familiar, well its the pattern for writing functions in php :)

 

funcName can be a-z lowercase or upper and any number of chars

list of arguments is the same except it includes some other special chars like ',' and '='

code body is just about anything and any number of chars

and it ends with a final }

 

how to differentiate between last brackets (}) of the code body than the real last bracket of the actual function, i'm clueless

 

but if you show me some regex to match that above describe pattern, I will most def. learn from it :)

 

-thank you

Link to comment
Share on other sites

I tried this:

 

<?php
$string = '
$bla = \'this other code\';
function funcName(list of arguments) {
if (lala) {
	maybe;
} else {
	ok;
}
return $set;
}';
$find = preg_match_all('/function ([a-zA-Z0-9_]*) \{(.*)\}[^}]*/', $string, $matches);
print_r($matches);?>

 

 

it returned this:

Array

(

[0] => Array

(

)

 

[1] => Array

(

)

 

[2] => Array

(

)

 

[3] => Array

(

)

 

)

 

 

 

what do I need to change?

Link to comment
Share on other sites

This should do what you want:

 

<?php

$string = 'function tisoglol($lol="dild", $laks="fersk")';

$regex = "/^function ([a-z]+)\(([a-z=, \"\$]*)\)$/i";

preg_match_all($regex,$string,$matches);

print_r($matches);
?>

 

It will match bad function syntax though since the argument part is only specified as a string of 0 or more occurrences of $, a-z, ", ,(comman).

 

So this will match too:

 

"function somefunction("lol=fer$sk)"

Link to comment
Share on other sites

thanks for the interest

 

but you've misunderstood me (I need to work on this)

 

your regex grabs the function name and the arguments of the function

I want to grab the function name, the arguments, and the code body of the function

in other words the whole pattern, like I mentioned in the above posts

 

verbatim what I mean is this:

$bla = \'this other code\';

function funcName(list of arguments) {

if (lala) {

maybe;

} else {

ok;

}

return $set;

}';

 

I would want to put

function funcName(list of arguments) {

code body

}

 

into an array, where in each array it grabs another pattern like this, this particular example (since there is only 1 function in the string) should produce an array with 1 element in it

 

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.