Complete expression

Asked

Viewed 79 times

3

Well, I have something like this:

/^perfil[\/]?([0-9]{3})?[\/]?([a-z]{4,5})?[\/]?/i

And I need to create a function that by receiving the right information turns this into

perfil/431/abcd/
perfil/431/abcd
perfil/431/
perfil/431
perfil/
perfil

However, this function must accept various types of expressions such as this, and transform them through the informed parameters. How could I do that? I came to see the function preg_replace but I don’t quite understand how to manipulate these expressions.

So what I need is a function that gets something like

/^perfil[\/]?([0-9]{3})?[\/]?([a-z]{4,5})?[\/]?/i
/^entrar[\/]?$/i
/^cadastro[\/]([0-9]{3})[\/]?$/i

And also receive the parameters of these expressions, for example, for

/^perfil[\/]?([0-9]{3})?[\/]?([a-z]{4,5})?[\/]?/i

I would send this expression, and send 0 to 2 parameters, to fill in ([0-9]{3}) and ([a-z]{4,5}):

$expressao = "/^perfil[\/]?([0-9]{3})?[\/]?([a-z]{4,5})?[\/]?/i";
$valores = array(999, "zzzz");
$retorno = funcao($expressao, $valores);

And return in this case would equal:

perfil/999/zzz

One more example

$expressao = "/^cadastro[\/]([0-9]{3})[\/]?$/i";
$valores = array(999);
$retorno = funcao($expressao, $valores);

And in this case, return would equal:

cadastro/999/

That is, I send the expression and the values I need to be put into it, and the function would return the full expression

  • insert in your question some examples of "due information" so that it is possible to test, otherwise how will we know what type of string your algorithm will be input? structure the question in a way that contains input examples with associated output examples. for example: you will receive a string "a/a/a" and I want my expression to transform "a/a/a" into "aaa"

  • I need the function to receive the raw expression as /^perfil[\/]?([0-9]{3})?[\/]?([a-z]{4,5})?[\/]?/i and the values that I need to be put in the gaps, for example, array(999, 'ssss'), and the function would return perfil/999/ssss/

  • Are you sure you need it and you want to use the regex to do what you want? it might be a lot simpler that way it’s gonna take some work, particularly I don’t know how to do it

  • It doesn’t necessarily have to be a regex, I thought that way it would be easier, I just need to implement this functionality anyway

  • What you want to do is a lexer analyzer, link to this, I recommend you do by token, and not regex, it will be simpler.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.