Results 1 to 3 of 3

Thread: Regular Expression to change string to array.

  1. #1

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Regular Expression to change string to array.

    Hi,
    I have a question about sorting the content of a string.

    I have a string that contains coordinates ...
    it looks like this:
    Code:
    "[aa][bc][sa][dd][qq][se][zd][dk][ks]"
    each coordinate looks like this:
    Code:
    [..]
    This string needs to be sorted so that I get the following result:
    Code:
    "[aa][bc][dd][dk][ks][sa][se][qq][zd]"
    What is the best way to do this?

    I was thinking to do it the following way:
    First I would parse all coordinates by using a for-loop and the substr function. And I would put them in an array. Next I would sort the array with the sort() function.

    So after the sorting the array would look like this:

    Code:
    aa
    bc
    dd
    dk
    ks
    sa
    se
    qq
    zd
    I was just thinking... Is there a faster way?

    I saw somebody solving a simular problem by using regular expressions:
    Code:
    $list = preg_split('/;\s*/', $str);
    The problem is: I forgot how to create regular expressions. I was just wondering, is there somebody who can spare me 2 hours of reading, studying and testing and give me the pattern . That would be very nice!

    Thank you very much in advance !
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Regular Expression to change string to array.

    Code:
    $numbers = explode('][', substr($str, 1, -1));
    Look, no regex! Which means it's probably about 10 times as fast.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member BramVandenbon's Avatar
    Join Date
    Jan 2002
    Location
    Belgium
    Posts
    502

    Re: Regular Expression to change string to array.

    Nice Job ! :-) Thank you
    ____________________________________________

    Please rate my messages. Thank you!
    ____________________________________________
    Bram Vandenbon
    http://www.bramvandenbon.com

Posting Permissions

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



Click Here to Expand Forum to Full Width