tags:

views:

47

answers:

4
(?P<id>\d*)(/(?P<title>.*))?

Most of the time,we use regex to match something,but how to generate the matching string if we have id and title already?

Example,if id=4 and title='hello world',the result should be:

4/hello world

But if we only have id=4,it should be:

4

Because as the regex indicates,title is optional.

Two answers both misunderstood...

There is no preg_match yet

A: 

how to get the original matched string if we have id and title already?

Original Matched string should be in

$0
S.Mark
No,there is no `preg_match` yet
Do you mean, before you match it, where is the matched string?
S.Mark
Sorry,edited my question.
Imm, for the lack of understanding on update one, let me delete my answer.
S.Mark
A: 

The whole regex match is always located in the group 0, so you can get this match in the matches array of preg_match() by accessing index 0 an/or using $0 or \0 in the replacement of preg_replace()

Progman
A: 

Not sure I understand the question, you mean constructing the string like this?

$string = $id;
if ( isset($title) ) {
    $string .= "/$title";
}
kemp
constructing the string based on `(?P<id>\d*)(/(?P<title>.*))?`,`$id` and `$title`
So you want to interpret a regular expression and act accordingly? I'll pass on writing a regexp parser :)
kemp
There should be another way around, yet to find.
Depends on how general that pattern can be. Or maybe the problem can be solved in another way (like inspecting how that pattern is built). We don't have enough information to provide alternative solutions.
kemp
We can match regex without writing a parser,right?I don't know if somehow PHP has cooked such a function ready for us..
Sure we can, that's why I said that it depends on how general that pattern can be. If - for example - only the labels change while the rest stays the same, than it's easy.
kemp
How?I guess you assumed it's always in `xx/xx` format,no you can't make that kind of assumption.
Hence my "for example". If it can by **any** regular expression then you need a full regexp parser/interpreter.
kemp
+1  A: 

You propose that if you pass the regular expression (?P<id>\d*)(/(?P<title>.*))? to a function along with the parameters id=4 and title='' that the function would return 4. And that this would work for any regular expression. That is simply impossible. Your regular expression is an example that such a function could never support.

If you call preg_match with your regular expression on the string 4 then the match results will return 4 for the capturing group id and the empty string for the capturing group title. If you call preg_match with the same regex on the string 4/ then the match results will also be 4 for the capturing group id and the empty string for the capturing group title. PHP does not differentiate between capturing groups that match the empty string and capturing groups that do not participate in the regex at all. Both return the empty string. Notice that in your regex you did not only make the group optional with the question mark, the .* inside your capturing group is also optional.

Thus, we have two possible matches 4 and 4/ for which preg_match returns 4 for the capturing group id and the empty string for the capturing group title. So how is your requested reverse function supposed to determine whether it should return 4 or 4/ for your two capturing groups? It can't be done without additional information.

In fact, do you realize your regex also matches / as well as the empty string? Everything in your regular expression is optional!

Jan Goyvaerts