Regular expressions question

by 4 replies
5
Hey everyone,

I'm trying to figure out how to write a regular expression that can find a hyperlink that contains a specific word somewhere in the link.

For example, lets say the word is "Product". It should find links like <a href="Products.html"> OR <a href="Product_List.php"> but NOT links like <a href="SomeOtherPage.html">, of course.

I found out how to use Regex to find regular hyperlinks by using href="(?<Link>.*?)"

But I don't know how to make it find hyperlinks that contain a specific word.

So, any regex pros out there know how to do this?
#programming #expressions #question #regular
  • $string1 = "Hello World\n";
    if ($string1 =~ m/rld$/) {
    print "$string1 is a line or string ";
    print "that ends with 'rld'\n";
    }

    This matches whether the string ends with 'rld'... I think this kind of Regex will be used... We are near to the result... I have tried to get some more of it... But couldnt. Please update if you get the answer.
    • [ 1 ] Thanks
    • [1] reply
    • Try this:

      <?php
      $url = "myproductsite.com";
      $pattern = "/product/";
      if (preg_match($pattern, $url)) {
      echo $url;
      }
      ?>
      • [ 1 ] Thanks
      • [1] reply

Next Topics on Trending Feed