Solved

convert sed to preg_match?

Posted on 2012-03-25
2
735 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

This article discusses four methods for overlaying images in a container on a web page
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

724 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