Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

reversing numbers regex

Last post 01-20-2012, 1:07 PM by ddrudik. 4 replies.
Sort Posts: Previous Next
  •  12-07-2011, 8:01 AM 84325

    reversing numbers regex

    hi

     

    I have paragraph that contains numbers in reverse and I need to un--reverse them.
    but any number in square brackets is not reversed and should not be flipped.

     

    it's something like this:

    "John was born in the seventies, 3791 to be exact , at the 72 of November (see reference [13])
    he was only 91 years old when he met and fell in love with Dana the Umbrella, even though she kept him high and dry"

     

    "3791" should be "1973", the "72" in "72 of November" should be "27 of November" and "91 years old" should be "19 years old".
    but the "13" in "(see reference [13])" is ok and should stay like that.

    is reversal even possible using regex ? because I don't know of one if it is multiply occurrences

     

     

     

  •  12-07-2011, 5:36 PM 84327 in reply to 84325

    Re: reversing numbers regex

    Which regex variant are you using? This is influence the possible answers you can get.

    IF you have a regex variant that allows a callback (or delegate) function, and IF the regex variant allows for lookaround operators then you can use a pattern such as:

    (?<!\[)(?>\d+)(?!\])

    to locate all of the digit sequences that are not preceded or followed by square brackets (this pattern would also stop " the cost is $123]" from being found as well - you may need to be very clear on exactly what the surrounding characters can or cannot be, including spaces etc) and then have the "match" callback function called with just the matched characters. You could then use normal string functions in the callback routine to reverse the order of the string and then use that to replace the match.

    The general rule is that a regex can be used to locate patterns of characters, but the manipulation of those characters is not something is can do beyond the trivial.

    Susan

  •  12-08-2011, 10:17 AM 84332 in reply to 84327

    Re: reversing numbers regex

    first off: thanks for the reply.
    I'm not sure what regex variant means, I'm using PHP code to do this...

    I need only 2 figures and above (not one) so I modified it to:
    (?<!\[)(?>\d\d+)(?!\])

    about the surrounding characters: it's an entire book I need to replaces numbers with and I don't know before hand what characters I need to exclude at this point, I will know as I run some regex and see which one came through that I still don't want.
    the square brackets I already know, but I see now that I will also need to exclude the follwoing:
    quotation mark  -  "   ,  as this pattern: "10448 gold box"
    and this type of backslash -   /  for this kind of pattern: bluerag/1998/124

    there might be more, once I know how to add more exclude characters I'll add them myself.....I tried to add "  and /   but failed :(

  •  12-08-2011, 5:31 PM 84334 in reply to 84332

    Re: reversing numbers regex

    RE the regex variant: there is no such thing as a "standard" when it comes to regexes. You have actually answered the question by saying that you are using PHP as this uses the PCRE regex library and that defines what the capabilities are. Another common variant is the .NET regex  which is available on the Windows platforms. Some users are working in a unix shell using tools such as awk, sed, grep and the like. Each of these has its own set of capabilities both in terms of the regex pattern operators it interprets as well as functionality such as callback routines.

    In your case, PHP would allow you to define a callback function that is called each time the "replace" regex function makes a match; the function is also able to return the string that can be used to replace the matched characters. Therefore you will need to write the callback function according to the rules for the parameters and return value (see the PHP documentation as I have not done this myself) and reference this in the "replace" statement along with the pattern you need to do the matching in the first place. The callback function can take the matched digits, use the string functions to reverse their order and then return them as the replacement string.

    I think you may have misunderstood what I was trying to say when i talked about the other characters that might surround the target digits. The pattern I suggested will ONLY find digits if they are both preceded and followed immediately by the opening and closing square brackets. Therefore the other situations you mention (leading double quotes or slashes etc.) will not be matched by this pattern. What I was trying to say is that if you DO need these matched (and therefore have the digits reversed) then you will need to modify the pattern to ALSO match in those cases. From what I can see you don't and so no changes will be needed.

    On the other hand, if there could be whitespace between the brackets and the digits (as in "[  123  ]" then this will also be missed by my pattern but this can be overcome if this situation occurs within your text.

    Not that is makes any practical difference, but if you want that there must be 2 or more digits, the you can use the syntax '\d{2,}' instead of '\d\d+'. In this case there is not much difference but if you wanted to say "3 or more digits", then "\d\d\d+' starts to get messy and it is easy to get lost as to the minimum number, whereas '\d{3,}' makes this very clear.

    Susan

  •  01-20-2012, 1:07 PM 84528 in reply to 84334

    Re: reversing numbers regex

    Using your pattern and Aussie Susan's suggestion of a callback, here's a sample how that might work:

    <?php
    $mystr="John was born in the seventies, 3791 to be exact , at the 72 of November (see reference [13])
    he was only 91 years old when he met and fell in love with Dana the Umbrella, even though she kept him high and dry"
    ;
    echo preg_replace_callback(
            '/(?<!\[)(?>\d\d+)(?!\])/',
            create_function(
                '$matches',
                'return strrev($matches[0]);'
            ),
            $mystr
        );
    ?>


View as RSS news feed in XML