none
Regex: help with a Replace string RRS feed

  • Question

  • Original Search of an XML file is
    <CREATED_ON>(?<Year>(20|19)\d\d)(?<Month>\d\d)(?<Day>\d\d)(?<Hours>\d\d)(?<Minutes>\d\d)(?<Seconds>\d\d)</CREATED_ON>

    Original Replace is
    <CREATED_ON>${Year}-${Month}-${Day} ${Hours}:${Minutes}:${Seconds}</CREATED_ON>

    -------------------------------------------------
    Issue: now I need to also search and replace a new tag, MODIFIED_ON. I can search for it fine, but I cannot figure out how to replace.

    The new search is as follows:
    <(CREATED|MODIFIED)_ON>(?<Year>(20|19)\d\d)(?<Month>\d\d)(?<Day>\d\d)(?<Hours>\d\d)(?<Minutes>\d\d)(?<Seconds>\d\d)</(CREATED|MODIFIED)_ON>

    What Is the New REPLACE? I understand that if I could use anchors it shouldn't matter whether the string was a CREATED_ON or a MODIFIED_ON element but I am having a hard time figuring out how to do it. Any help appreciated.


    HomeCookN

    Friday, March 1, 2013 6:56 PM

Answers

  • Have you tried this?

    Pattern: <(?<Tag>(CREATED|MODIFIED)_ON)>(?<Year> . . . .

    Replace: <${Tag}>${Year} . . . . </${Tag}>

    • Marked as answer by HomeCookN Friday, March 1, 2013 7:14 PM
    Friday, March 1, 2013 7:06 PM
  • If tags do not have attributes, then try ‘\k’:

    <(?<Tag>.+?)>(?<Year>(20|19)\d\d)(?<Month>\d\d)(?<Day>\d\d)(?<Hours>\d\d)(?<Minutes>\d\d)(?<Seconds>\d\d)</\k<Tag>>

    The replacement string remains unchanged.

    Or you can simply search for “>(?<Year>(20|19)\d\d)(?<Month>\d\d)(?<Day>\d\d)(?<Hours>\d\d)(?<Minutes>\d\d)(?<Seconds>\d\d)</”.

    • Marked as answer by HomeCookN Friday, March 1, 2013 9:25 PM
    Friday, March 1, 2013 7:54 PM

All replies

  • Have you tried this?

    Pattern: <(?<Tag>(CREATED|MODIFIED)_ON)>(?<Year> . . . .

    Replace: <${Tag}>${Year} . . . . </${Tag}>

    • Marked as answer by HomeCookN Friday, March 1, 2013 7:14 PM
    Friday, March 1, 2013 7:06 PM
  • Great, Viorel; this works fine. Thank you very much....

    Now, for extra credit, I have 1 additional question: how could I change the search so that instead of returning the entire line, e.g.,  <CREATED_ON>20041123085000</CREATED_ON>, it would return only the 14 digits in the middle, i.e., 20041123085000 so that I could just replace only them without worrying about the XML opening and closing tags?


    HomeCookN

    Friday, March 1, 2013 7:20 PM
  • If tags do not have attributes, then try ‘\k’:

    <(?<Tag>.+?)>(?<Year>(20|19)\d\d)(?<Month>\d\d)(?<Day>\d\d)(?<Hours>\d\d)(?<Minutes>\d\d)(?<Seconds>\d\d)</\k<Tag>>

    The replacement string remains unchanged.

    Or you can simply search for “>(?<Year>(20|19)\d\d)(?<Month>\d\d)(?<Day>\d\d)(?<Hours>\d\d)(?<Minutes>\d\d)(?<Seconds>\d\d)</”.

    • Marked as answer by HomeCookN Friday, March 1, 2013 9:25 PM
    Friday, March 1, 2013 7:54 PM
  • Thanks a lot, Viorel! The \k idea will be great. Can't use the simplier second suggestion because my data set has lots of 20 digit numbers which are not datetime and they should not be found. I really appreciate your help. Have a good one.

    HomeCookN

    Friday, March 1, 2013 9:28 PM