Solved

convert sed to preg_match?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
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.:
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.

707 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

17 Experts available now in Live!

Get 1:1 Help Now