Conditional replacing



  • So I have a document that will have a number for each member number we will say its 1234567 (always the same base member number)
    about 6 rows below that will be a row it will find a identifier like PRM or SEL
    I want it to then go back to that member number and add (replace to 123456789) 89 to the member number if is identifies PRM, else if it is SEL then I want it to add (replace to 123456700) 00 to the member number. Ideas?



  • There is conditional syntax in regex (both in match and replacement), so it’s not impossible.

    But to be helpful, we would need dummy data. Please show us something that would be representative of the actual data (but of course no sensitive info), and preferrably show us both a before and an after set of data.

    To include the data in your post, paste it in, highlight it, and use the </> button on the toolbar:
    b9aa1100-d3b5-44fa-a79b-408c6c56e030-image.png
    Keep the data separate, like
    BEFORE:

    here is data
    

    AFTER:

    there went my data. whoops.
    


  • Hi @William-Baldwin, @PeterJones

    This regex could do the job, but lacking of actual data, as @PeterJones noted, may make it fail. Take it as a proof of concept:

    1234567
    bla
    bla
    bla
    bla
    bla
    PRM
    1234567
    bla
    bla
    bla
    bla
    bla
    SEL
    

    Search: (?-s)^(\d{7})(?s).*?(?:(PRM)|(SEL))
    Replace: (?2${1}89)(?3${1}00)

    Output:

    123456789
    123456700
    

    Hope this helps.


Log in to reply