REGEXP problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frizzle

    REGEXP problem

    Hi there,

    I'm not really experienced with regexpes etc. so that's why i come
    here.
    My script needs to parse the requested filename:

    Imagine the filename is index_en.html
    My output needs to be an array with the following:
    language =en (no underscore)
    file =index (no "_en")
    extension =html

    i created the following, but i don't know how to move on:
    preg_split( '/^[a-z0-9]*(_(en|_ru))?\.[a-z0-9]{2,4}$/', $filename );

    As you can see, the _en part can be left out (index.html) wich would
    have to return
    language =default

    I hope it's clear, cause i really need to get this going!

    Thanks.

    Frizzle.

  • Erwin Moller

    #2
    Re: REGEXP problem

    frizzle wrote:
    Hi there,
    >
    I'm not really experienced with regexpes etc. so that's why i come
    here.
    My script needs to parse the requested filename:
    >
    Imagine the filename is index_en.html
    My output needs to be an array with the following:
    language =en (no underscore)
    file =index (no "_en")
    extension =html
    >
    i created the following, but i don't know how to move on:
    preg_split( '/^[a-z0-9]*(_(en|_ru))?\.[a-z0-9]{2,4}$/', $filename );
    >
    As you can see, the _en part can be left out (index.html) wich would
    have to return
    language =default
    >
    I hope it's clear, cause i really need to get this going!
    >
    Thanks.
    >
    Frizzle.


    Hi,

    This part
    (_(en|_ru))?
    matches
    _en or __ru or nothing (note the double _)

    I doubt that is what you want.
    I expect you mean:
    (_(en|ru))?
    which will match
    _en
    _ru
    nothing

    Unless you want to learn regexpr (which is fun), I think it is easier to use
    explode:
    $filename = "blabla_en.html ";
    $parts = explode(".",$fi lename);
    $otherparts = explode("_",par ts[0]);

    $extension = $parts[1];
    $file = $otherparts[0];
    if (count($otherpa rts) == 1){
    $language = "Default";
    } else {
    $language = $otherparts[1];
    }

    It is more code, but easier to understand. :-)

    Regards,
    Erwin Moller

    Comment

    • John Dunlop

      #3
      Re: REGEXP problem

      frizzle:
      Imagine the filename is index_en.html
      My output needs to be an array with the following:
      language =en (no underscore)
      file =index (no "_en")
      extension =html
      Making some assumptions (untested):

      <^([^_.]*)(_([^.]*))?\.(.*)$>i

      Applied to "index_en.html" :

      0 = "index_en.h tml"
      1 = "index"
      2 = "_en"
      3 = "en"
      4 = "html"

      Applied to "index.html ":

      0 = "index.html "
      1 = "index"
      2 = ""
      3 = ""
      4 = "html"

      --
      Jock

      Comment

      • Pedro Graca

        #4
        Re: REGEXP problem

        frizzle wrote:
        Imagine the filename is index_en.html
        My output needs to be an array with the following:
        language =en (no underscore)
        file =index (no "_en")
        extension =html
        >
        i created the following, but i don't know how to move on:
        preg_split( '/^[a-z0-9]*(_(en|_ru))?\.[a-z0-9]{2,4}$/', $filename );
        preg_match() is your friend.

        if (preg_match('/^([a-z0-9]*)(?:_(en|ru))? \.([a-z0-9]{2,4})$/', $filename, $matches)) {
        $output['language'] = $matches[2];
        $output['file'] = $matches[1];
        $output['extension'] = $matches[3];
        }


        Reference: http://www.php.net/preg_match

        --
        File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

        Comment

        • frizzle

          #5
          Re: REGEXP problem


          Pedro Graca wrote:
          frizzle wrote:
          Imagine the filename is index_en.html
          My output needs to be an array with the following:
          language =en (no underscore)
          file =index (no "_en")
          extension =html

          i created the following, but i don't know how to move on:
          preg_split( '/^[a-z0-9]*(_(en|_ru))?\.[a-z0-9]{2,4}$/', $filename );
          >
          preg_match() is your friend.
          >
          if (preg_match('/^([a-z0-9]*)(?:_(en|ru))? \.([a-z0-9]{2,4})$/', $filename, $matches)) {
          $output['language'] = $matches[2];
          $output['file'] = $matches[1];
          $output['extension'] = $matches[3];
          }
          >
          >
          Reference: http://www.php.net/preg_match
          >
          --
          File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
          Thanks all.

          I'll use John's or Pedro's way.
          I am a little bit familiar with regex, but it's stuffed away somewhere
          down there, and i can't reach it. Yet!!
          Anyway, thanks a lot for your help!!

          Frizzle.

          Comment

          Working...