Solved

convert sed to preg_match?

Posted on 2012-03-25
2
727 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 110

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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