0
(?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

4 Answers 4

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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

Original Matched string should be in

$0

3 Comments

No,there is no preg_match yet
Do you mean, before you match it, where is the matched string?
Imm, for the lack of understanding on update one, let me delete my answer.
0

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

Comments

0

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

$string = $id;
if ( isset($title) ) {
    $string .= "/$title";
}

8 Comments

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 :)
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.
We can match regex without writing a parser,right?I don't know if somehow PHP has cooked such a function ready for us..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.