Solved

convert sed to preg_match?

Posted on 2012-03-25
2
707 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 108

Accepted Solution

by:
Ray Paseur earned 500 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now