perl regexp

Discussion in 'Programming' started by cujo, Jul 24, 2013.

  1. #1
    I am trying to insert "http://" on URL's that do not have it so links are created properly (and to ignore URL's with "https://"). I have the below, but it no worky.

    if (! $company_url =~ /^(http|https):\/\//) { $company_url = 'http://' . $company_url; }
    Code (markup):

     
    cujo, Jul 24, 2013 IP
  2. cujo

    cujo Well-Known Member

    Messages:
    638
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    170
    #2
    Below seems to work...
        if ( $company_url =~ /^(http|https):\/\//) {
        } else {
          $company_url = 'http://' . $company_url;
        }
    
    Code (markup):
     
    cujo, Jul 24, 2013 IP
  3. Marcel Preda

    Marcel Preda Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    43
    #3
    Hi there,

    I guess it has something to do with the operators precedence.
    in the 1st example the !$company_url is evaluated 1st, and at the ene the =~ is done.

    You can use
    if ( $company_url !~ /.../ )
    or
    if ( !($company_url =~ /.../))

    BR,
    Marcel
     
    Marcel Preda, Jul 29, 2013 IP
  4. DieterW

    DieterW Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    $company_url !~ /^(http|https)\:\/\// && ($company_url = 'http://' . $company_url);
    Code (markup):
     
    DieterW, Aug 6, 2013 IP