Thursday, 15 September 2011

Keep case with regex find and replace -


My question is very simple to use only to locate and replace a regular expression, whether the case of the original words It is possible to keep

So if I have a string: "Beautiful beautiful is very beautiful" How can I change it: "Lovely beautiful is very cute"

I still find Change with / (P | P) retty / g and $ 1ovely but how do I replace L with L and replace with the lowercase P in the place of Capital P?

I have no interest in completing this in a particular language, I have to know whether it is possible to do it with pure Regedes.

To replace captured uppercase or lowercase letters, it is unlikely possible that only Reggae Letters according to the type of letter captured through But the language can be possible through the built-in functions + regex.

In php, I would like to do this.

  $ str = "Beautiful is pretty much"; Echo preg_replace_callback ('~ ([pP]) retty ~', function ($ m) {if ($ m [1] == "P") {return "Lovely";} and {return "beautiful";}}, $ Str);  

Output:

  Lovely is pretty beautiful  

No comments:

Post a Comment