?
Solved

preg_replace difference

Posted on 2011-10-12
3
Medium Priority
?
217 Views
Last Modified: 2012-05-12
hello there,
I would like to know whats the difference between these two regex
$title = preg_replace("#^(.*?)[([]?(\d{4}).*$#", "$1($2)",$title);
and
$title = preg_replace("#([^0-9]+)([0-9]{4})#", "$1($2)",$title);

Open in new window

0
Comment
Question by:XK8ER
  • 3
3 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36956940
The first one involves the entire string from start ^ to end $
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36956946
What are you trying to achieve?  I may be able to help.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36957073
I annotated the second regex.  It looks like it puts parentheses around substrings of four digits.
http://www.laprbass.com/RAY_temp_xk8er.php Outputs:
string(21) "#([^0-9]+)([0-9]{4})#"
string(109) "LOTS OF NON-NUMERIC STUFF FOLLOWED BY 333 DIGITS, OR 4444 DIGITS AND MAYBE 55555 DIGITS? WHAT ABOUT 88888888?"
string(115) "LOTS OF NON-NUMERIC STUFF FOLLOWED BY 333 DIGITS, OR (4444) DIGITS AND MAYBE (5555)5 DIGITS? WHAT ABOUT (8888)8888?"

You might enjoy this article.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Best regards, ~Ray
<?php // RAY_temp_xk8er.php
error_reporting(E_ALL);
echo "<pre>";

// FROM THE POST AT EE
// $title = preg_replace("#([^0-9]+)([0-9]{4})#", "$1($2)",$title);

$rgx
= '#'            // REGEX DELIMITER
. '('            // START OF A GROUP
. '['            // START OF A CHARACTER CLASS
. '^'            // NEGATION - MATCH NONE OF THE CHARACTERS IN THE CLASS
. '0-9'          // NUMBERS
. ']'            // END OF THE CHARACTER CLASS
. '+'            // MATCH ONE OR MORE
. ')'            // END OF THE GROUP
. '('            // START OF THE NEXT GROUP
. '[0-9]'        // CHARACTER CLASS OF ONLY NUMBERS
. '{4}'          // EXACTLY FOUR CHARACTERS
. ')'            // END SECOND GROUP
. '#'            // END REGEX DELIMITER
;

// SOME TEST DATA
$str = 'LOTS OF NON-NUMERIC STUFF FOLLOWED BY 333 DIGITS, OR 4444 DIGITS AND MAYBE 55555 DIGITS? WHAT ABOUT 88888888?';

// RUN THE EXPRESSION AND SHOW THE INFORMATION
$new = preg_replace($rgx, "$1($2)", $str);
var_dump($rgx);
var_dump($str);
var_dump($new);

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question