views:

1145

answers:

4

I have a string that I want to search through:

"Query Result: Val for ?f : http://www.rk.com/#steak Val for ?a : http://www.rk.com/#movietheaters"

The two variables that I want to capture are the two words after the hashes (in this case steak and movietheaters). Note that there will always be an uri infront of the #. I know I can easily capture groups in .NET or Java but I want to use php. Unfortunately I'm stumbling on the syntax for the regex that would match the string above with the correct capture groups.

A: 

Try:

 /http.*?\/#([^\s])/
thedz
+2  A: 
$sites = "Query Result: Val for ?f : http://www.rk.com/#steak Val for ?a : http://www.rk.com/#movietheaters";

preg_match_all('/\.[a-zA-Z]{2,4}\/#([a-z-A-Z0-9]+)/', $sites, $matches);

var_dump($matches[1]);

//Output
array
  0 => string 'steak' (length=5)
  1 => string 'movietheaters' (length=13)
Nathan
A: 
$str = "Query Result: Val for ?f : http://www.rk.com/#steak Val for ?a : http://www.rk.com/#movietheaters sdf";
# first , split on #
$s = explode("#",$str);
# go through the elements
for ($i=1;$i<count($s);$i++){
    # split on splace
    $z = explode(" ",$s[$i]);
    #get the first element.
    print $z[0] ."\n";
}

output

# php5 test.php
steak
movietheaters
ghostdog74
He asked for regular expressions, not madness.
hobodave
regular expressions are madness as well...
ghostdog74
+1  A: 

This is as simple as:

preg_match_all('/#(\w+)/', $sites, $matches);

var_dump($matches[1]);

array
  0 => string 'steak' (length=5)
  1 => string 'movietheaters' (length=13)
hobodave