Find and duplicate the date in the file

Find and duplicate the date in the file

2
NewbieNewbie
2

    Nov 19, 2013#1

    Hello all,

    My apology in advance if this is the wrong board to ask.

    I have a text file with about 2,000 lines of following data, e.g.
    ==============================================================
    Mary was born on 2/13/70 and she was attending the Greenwood elementary school.
    John was born on 11/4/56 and she was attending the Eastside elementary school.
    ==============================================================

    I need to be able to copy the "date" in the sentence to the beginning of the sentence and add a blank space after the "date".
    So, each line will look like:

    =====================================================================
    2/13/70 (space) Mary was born on 2/13/70 and she was attending the Greenwood elementary school.
    11/4/56 (space) John was born on 11/4/56 and she was attending the Eastside elementary school.
    =====================================================================

    How do I create the macro to achieve this result? I appreciate all your helps!

    6,682583
    Grand MasterGrand Master
    6,682583

      Nov 19, 2013#2

      There is no need for a macro for this task.

      A Perl regular expression Replace All executed from top of the file with search string ^(.*?)(\d+/\d+/\d+) and the replace string \2 \1\2 does this job.

      Explanation:

      ^ ... start every search at beginning of a line.

      (...) ... whatever is found by the expression inside the parentheses, tag it (temporarily store it) and re-use it for the following replace instead of \1 in the replace string.

      .*? ... find any character except new line characters (.) 0 or more times (*) non greedy (?). Non greedy means select as less as possible to get a positive match on next part of the search expression.

      (...) ... whatever is found by the expression inside the parentheses, tag it and re-use it for the following replace instead of \2 in the replace string.

      \d+ ... 1 or more digits (0-9).
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Nov 19, 2013#3

        Thanks so much for the code and detailed explanation, Mofi!!

        Maybe you heard this many times but you are definitely a great person and an invaluable asset to the board!!

        6,682583
        Grand MasterGrand Master
        6,682583

          Nov 19, 2013#4

          gomax wrote:Maybe you heard this many times but you are definitely a great person and an invaluable asset to the board!!
          Such kind words are very rare on this board. For that reason I'm very happy about all such replies after helping a user.

          The majority of users asking for help does not even post a short and simple Thanks or give any other feedback to the person who replied and to all others reading the topic who perhaps want to know also if the reply was helpful.

          Thank you very much for your kind and encouraging feedback.
          Best regards from an UC/UE/UES for Windows user from Austria