Jump to content

regular expression


Jeyalakshmi

Recommended Posts

<?php$string = "This is a test";echo str_replace(" is", " was", $string);echo ereg_replace("( )is", "\\1was", $string);echo ereg_replace("(( )is)", "\\2was", $string);?> output is :This was a testThis was a testThis was a testcan any one know how this has come

Link to comment
Share on other sites

It seems like you're trying to perform a simple string replace function with this code, why not use PHP's string replace function? If that's not the case, you'll have to more clearly tell us what's happening and what you want to happen.

Link to comment
Share on other sites

<?php$string = "This is a test";echo str_replace(" is", " was", $string);echo ereg_replace("( )is", "\\1was", $string);echo ereg_replace("(( )is)", "\\2was", $string);?>
You're telling it to do the same thing 3 times, which is always to replace " is" with " was". The first replace is straightforward. The second replace uses parentheses to specify that the space is a subgroup (which isn't necessary for this example), and the replacement is the word "was" preceded by the same pattern that "is" matched on. Since it can only match on a space, the replacement will be " was". The third replacement is doing the same thing, but it also creates a second subgroup out of the entire thing, and once again the replacement matces with " was".What do you expect to happen?
Link to comment
Share on other sites

You're telling it to do the same thing 3 times, which is always to replace " is" with " was".  The first replace is straightforward.  The second replace uses parentheses to specify that the space is a subgroup (which isn't necessary for this example), and the replacement is the word "was" preceded by the same pattern that "is" matched on.  Since it can only match on a space, the replacement will be " was".  The third replacement is doing the same thing, but it also creates a second subgroup out of the entire thing, and once again the replacement matces with " was".What do you expect to happen?

i cant understand that sub group and all could you please explain me more clearly
Link to comment
Share on other sites

A subpattern is anything inside parentheses. It is used to break up the regular expression into pieces, and you can refer to each piece later on. The "\1was" refers to the first subpattern, which is a space, followed by "was". The "\2was" refers to the second subpattern, which is a space, followed by "was". The reason the space is the second subpattern instead of the first subpattern in the third example is because the first subpattern in that example is the entire pattern, which is also surrounded by parentheses.If you are trying to learn about regular expressions, you will want to read this:http://www.php.net/manual/en/reference.pcr...tern.syntax.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...