Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

convert sed to preg_match?

Posted on 2012-03-25
2
Medium Priority
?
763 Views
Last Modified: 2012-08-13
How do I convert an sed expression into a php preg_match search?

The sed search is:   sed -n "s/.*name='\([^']*\).*/\1/p"

I need to do the equivalent with php using preg_match or a similar function.
0
Comment
Question by:pillmill
2 Comments
 
LVL 12

Expert Comment

by:larsrohr
ID: 37762300
preg_replace may suit your needs.  You won't need to escape the grouping parentheses.
$mystring = "something name='pillmill' etc";
$newstring = preg_replace("/.*name='([^']*).*/", "$1", $mystring);

Open in new window

0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37762458
I think this might be what you want.
http://www.laprbass.com/RAY_temp_pillmill.php

You might also find this article interesting, if you have some time to read.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

HTH, ~Ray

<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);


// TEST DATA
$str = <<<STR
noise name='pillmill',
chatter name="Paseur" etc, etc...
STR;


// A REGULAR EXPRESSION, SEE http://php.net/manual/en/reference.pcre.pattern.syntax.php
$rgx
= '#'         // REGEX DELIMITER
. 'name='     // EXPLICIT STRING
. '(\'|")'    // GROUP: SINGLE OR DOUBLE QUOTE
. '(.*?)'     // GROUP: ANYTHING UNGREEDY
. '(\'|")'    // GROUP: SINGLE OR DOUBLE QUOTE
. '#'         // REGEX DELIMITER
. 'i'         // CASE INSENSITIVE
;

// SEE http://php.net/manual/en/function.preg-match-all.php
if (preg_match_all($rgx, $str, $mat))
{
    // MATCHED NAMES ARE IN ARRAY POSITION THREE - THE SECOND GROUP
    var_dump($mat[2]);
}
else
{
    echo "NO MATCHES FOUND";
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

963 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