www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: Getting delimiter from a line

  1. #16
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    Just to be pedantic, CSV files use commas as delimiters (that's what the "C" stands for), with pretty specific rules for things like using quotes.

    I stole parts of what I used to identify the email address from here: http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html

    The only parts you should have to mess around with are this at the beginning:
    Code:
    (^|\S)\s*
    ...and this at the end...
    Code:
    \s*(\S|$)
    \s is any white-space (space, tab, newline, carriage return, vertical tab)
    \S is any character that is NOT a white-space character

    To include tabs as delimiters, I believe you could change those parenthesized bits to include the tab character via \t:
    Code:
    (^|[\S\t])\s*
    Code:
    ([\S\t]|$)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  2. #17
    Join Date
    Sep 2006
    Posts
    655
    Hi

    This is the regex that i am using after modification

    static $regex = '/(^|[\S\t])\s*[^()<>@,;:\\".\[\] \000-\031][^()<>@,;:\\"\[\] \000-\031]*@[^()<>@,;:\\"\[\] \000-\031]*[^()<>@,;:\\".\[\] \000-\031]+\s*([\S\t]|$)/';
    But the function is returning false when I am passing data with tab separated.

    Last edited by phantom007; 06-23-2013 at 05:44 AM.

  3. #18
    Join Date
    Sep 2006
    Posts
    655
    Apart from the issue reported in my last post, could you also split up your regex into variables so that if tomorrow I want to add a new character to allow/disallow I should be able to do it myself?

    Thanks for the help

  4. #19
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,637
    PHP Code:
        static $regex '/(^|[\S\t]) *?[^()<>@,;:\\".\[\] \000-\031][^()<>@,;:\\"\[\] \000-\031]*@[^()<>@,;:\\"\[\] \000-\031]*[^()<>@,;:\\".\[\] \000-\031]+ *([\S\t]|$)/'
    However, this probably won't work as desired if you have an odd case where the separator is some character like ";" but you also have a tab character before/after it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #20
    Join Date
    Sep 2006
    Posts
    655
    ok, I have updated the code and it still does not show the delimiter is tab.


    Here is the code and attached is the csv i m testing.

    Code:
      function getDelimiter($str, $debug = false)
        {
            static $regex = '/(^|[\S\t]) *?[^()<>@,;:\\".\[\] \000-\031][^()<>@,;:\\"\[\] \000-\031]*@[^()<>@,;:\\"\[\] \000-\031]*[^()<>@,;:\\".\[\] \000-\031]+ *([\S\t]|$)/';  
    
            if(preg_match($regex, $str, $matches)) {
                if($debug) {
                    echo "<pre>Degbug: found email:\n".var_export($matches,1)."</pre>\n";
                }
                for($ix=1; $ix<=2; $ix++) {
                    if(!empty($matches[$ix]) and preg_match('/\W/', $matches[$ix])) {
                        $delimiter = $matches[$ix];
                        $delimiter = ord($delimiter)==9 ? 'TAB' : $delimiter;
                        return $delimiter;
                    }
                }
            }
    
            return false;
        }

    Thanks
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles