Regex problem

Discussion in 'Javascript' started by Doris, Feb 22, 2010.

  1. Doris

    Doris Guest

    A list of points, defined by x and y coordinates, is to reversed. The list
    may be three to five points long. So for example

    list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
    list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'

    I am looking for a regular expression which will capture all these possible
    lengths in one pattern, but am having trouble removing and adding back in
    the commas. I can do it with three calls to the replace method, but there
    must be a better, more flexible and elegant solution:

    newlist = listx.replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    '$3,$2,$1' ).replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    '$4,$3,$2,$1' ).replace(
    /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/, '$5,$4,$3,$2,$1' );

    Any hints?

    Thanks,
    Doris
    Doris, Feb 22, 2010
    #1
    1. Advertising

  2. Doris

    Stevo Guest

    Doris wrote:
    > A list of points, defined by x and y coordinates, is to reversed. The list
    > may be three to five points long. So for example
    >
    > list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
    > list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'
    >
    > I am looking for a regular expression which will capture all these possible
    > lengths in one pattern, but am having trouble removing and adding back in
    > the commas. I can do it with three calls to the replace method, but there
    > must be a better, more flexible and elegant solution:
    >
    > newlist = listx.replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    > '$3,$2,$1' ).replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    > '$4,$3,$2,$1' ).replace(
    > /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/, '$5,$4,$3,$2,$1' );
    >
    > Any hints?
    >
    > Thanks,
    > Doris


    Much easier with code if you ask me.

    function reverseCoords(s)
    {
    var ar=s.split(",");
    var ret="";
    for(var i=ar.length-2;i>=0;i-=2)
    {
    ret+=ar+","+ar[i+1];
    ret+=i>0?",":"";
    }
    return ret;
    }
    Stevo, Feb 22, 2010
    #2
    1. Advertising

  3. Doris

    Evertjan. Guest

    Stevo wrote on 22 feb 2010 in comp.lang.javascript:

    > Doris wrote:
    >> A list of points, defined by x and y coordinates, is to reversed. The
    >> list may be three to five points long. So for example
    >>
    >> list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
    >> list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'
    >>
    >> I am looking for a regular expression which will capture all these
    >> possible lengths in one pattern, but am having trouble removing and
    >> adding back in the commas. I can do it with three calls to the
    >> replace method, but there must be a better, more flexible and elegant
    >> solution:
    >>
    >> newlist = listx.replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    >> '$3,$2,$1' ).replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    >> '$4,$3,$2,$1' ).replace(
    >> /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    >> '$5,$4,$3,$2,$1' );
    >>
    >> Any hints?
    >>
    >> Thanks,
    >> Doris

    >
    > Much easier with code if you ask me.
    >
    > function reverseCoords(s)
    > {
    > var ar=s.split(",");
    > var ret="";
    > for(var i=ar.length-2;i>=0;i-=2)
    > {
    > ret+=ar+","+ar[i+1];
    > ret+=i>0?",":"";
    > }
    > return ret;
    >}


    Without any code loping or local variable, any length:

    function reverseCoords(s) {
    return s.replace(/(\d+),(\d+)/g,'$1x$2').split(",").
    reverse().join(',').replace(/x/g,',');
    };


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Feb 22, 2010
    #3
  4. Doris wrote:

    > A list of points, defined by x and y coordinates, is to reversed. The
    > list may be three to five points long. So for example
    >
    > list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
    > list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'
    >
    > I am looking for a regular expression which will capture all these
    > possible lengths in one pattern, but am having trouble removing and
    > adding back in the commas. I can do it with three calls to the replace
    > method, but there must be a better, more flexible and elegant solution:
    >
    > newlist = listx.replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    > '$3,$2,$1' ).replace( /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/,
    > '$4,$3,$2,$1' ).replace(
    > /^(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+),(\d+,\d+)$/, '$5,$4,$3,$2,$1'

    );
    >
    > Any hints?


    A)

    var m,
    rxCoord = /\d+,\d+/g,
    newlist = [];

    while ((m = rxCoord.exec(listx)))
    {
    newlist.unshift(m[0]);
    }

    /*
    * Can be omitted if you use `newlist' only in place of a string,
    * as newlist.toString() === newlist.join() === newlist.join(",").
    */
    newlist = newlist.join();

    B)

    var
    items = listx.split(","),
    newlist = [];

    for (var i = items.length; (i -= 2) > -1;)
    {
    /* works here because slice() returns an Array reference */
    newlist.push(items.slice(i, i+2));
    }

    /* see above */
    newlist = newlist.join();

    C)

    var
    items = listx.split(","),
    newlist = [];

    while (items.length)
    {
    var y = items.pop();
    var x = items.pop();
    newlist.push(x, y);
    }

    /* see above */
    newlist = newlist.join();

    I would not be storing the coordinates in a string value to begin with,
    instead in an Array of the form [[x1, y1], [x2, y2], ...], but you might
    not have that choice.


    HTH

    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Feb 23, 2010
    #4
  5. Thomas 'PointedEars' Lahn wrote:

    > Doris wrote:
    >> A list of points, defined by x and y coordinates, is to reversed. The
    >> list may be three to five points long. So for example
    >>
    >> list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
    >> list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'
    >> [...]
    >> Any hints?

    >
    > [A) to C)]


    D)

    var newlist = listx.match(/\d+,\d+/g).reverse().join();

    :)


    PointedEars
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
    Thomas 'PointedEars' Lahn, Feb 23, 2010
    #5
  6. Doris

    Doris Guest

    "Thomas 'PointedEars' Lahn" said...
    >> Doris wrote:
    >>> A list of points, defined by x and y coordinates, is to be reversed.
    >>> The list may be three to five points long. So for example
    >>> list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
    >>> list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'

    >
    > var newlist = listx.match(/\d+,\d+/g).reverse().join();
    >
    > :)
    >
    > PointedEars


    Naturally..
    A quick follow up question (that I really should not be bothering you with
    before trying myself, but I have little to no idea where to begin atm): what
    would be the simplest method of determining the direction of travel along
    those points, ie. clockwise or counterclockwise, or figure-of-eight-wise if
    there are more than three?.
    Thank you all very much. It has been a fruitful endeavour so far.
    Doris
    Doris, Feb 23, 2010
    #6
  7. Doris wrote:

    > A quick follow up question (that I really should not be bothering you
    > with before trying myself, but I have little to no idea where to begin
    > atm):
    > what would be the simplest method of determining the direction of travel
    > along those points, ie. clockwise or counterclockwise, or
    > figure-of-eight-wise if there are more than three?.


    Sorry, I really won't do *all* of your homework. It is there for
    you to repeat and apply what you should have learned in class.

    > Thank you all very much. It has been a fruitful endeavour so far.


    You are welcome. Please fix your From header, though.


    PointedEars
    --
    realism: HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness: XHTML 1.1 as application/xhtml+xml
    -- Bjoern Hoehrmann
    Thomas 'PointedEars' Lahn, Feb 23, 2010
    #7
  8. In comp.lang.javascript message <4b832ffc$0$93437$.
    nl>, Tue, 23 Feb 2010 02:31:28, Doris <> posted:
    >>> Doris wrote:
    >>>> A list of points, defined by x and y coordinates, is to be reversed.
    >>>> The list may be three to five points long. So for example
    >>>> list3 = '1,2,3,4,5,6'; // should give '5,6,3,4,1,2'
    >>>> list4 = '1,2,3,4,5,6,7,8'; // will get '7,8,5,6,3,4,1,2'


    To your first question : RegExp /.*/ will capture all of those, so use
    newlist = oldlist.replace(/.*/, F)
    with a suitable function F <g>.


    >A quick follow up question (that I really should not be bothering you with
    >before trying myself, but I have little to no idea where to begin atm): what
    >would be the simplest method of determining the direction of travel along
    >those points, ie. clockwise or counterclockwise, or figure-of-eight-wise if
    >there are more than three?.


    Remember that they could be collinear. You could be talking about as
    seen from the origin, but I expect you mean just referring to the line
    itself.

    Call the points 1 2 3. Math.atan2(y2-y1, x2-x1) gives the direction of
    the first segment, similarly for the second. Find the difference,
    adjust it by a multiple of 2 pi to be in the range (-pi, +pi). If you
    get 0 they are collinear, if you cannot do it (because it is -pi) they
    are anti-collinear.

    If you know that X (or Y) increases/decreases monotonically, there's an
    easier way; extrapolate from 1 to 2 to x3, and see if y3 is too high or
    too low.

    It's too late to test either way in code tonight.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr J R Stockton, Feb 24, 2010
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    700
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,623
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    597
  4. Xah Lee
    Replies:
    1
    Views:
    942
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    761
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page