Community
    • Login

    Need Help to replace a text by finding other text as keyword

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 4 Posters 224 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Hendrick TheaH
      Hendrick Thea
      last edited by

      i want to find specific word but using other text as keyword. is it possible?

      For Ex:

      -Name : David
      Age : 20
      Sex : Male
      -Name : Sara
      Age : 23
      Sex : Female
      -Name : Felicia
      Age : 21
      Sex : Female
      -Name : Erick
      Age : 25
      Sex : Male
      

      i want to replace all Age to 30 by finding keyword male.
      thanks

      Paul WormerP 1 Reply Last reply Reply Quote 0
      • Paul WormerP
        Paul Wormer @Hendrick Thea
        last edited by

        @Hendrick-Thea
        Find:

        (?-s)(Age\h+:\h+)(\d{1,2})(.*\RSex\h+:\h+Male)
        

        Replace:

        ${1}30${3}
        

        Assumption: no male is older than 99 years.

        1 Reply Last reply Reply Quote 1
        • guy038G
          guy038
          last edited by

          Hello, @hendrick-thea, @paul-wormer and All,

          An alternate solution, which allow leading indentation :

          So, from this INPUT tet :

          -Name : David
          Age : 20
          Sex : Male
          -Name : Sara
          Age : 23
          Sex : Female
          -Name : Felicia
          Age : 21
          Sex : Female
              -Name : Erick
              Age : 25
              Sex : Male
          

          The following regex S/R :

          SEARCH (?<=Age : )\d+(?=.*\R\h*Sex : Male)

          REPLACE 30

          Would produce this OUTPUT text :

          -Name : David
          Age : 30
          Sex : Male
          -Name : Sara
          Age : 23
          Sex : Female
          -Name : Felicia
          Age : 21
          Sex : Female
              -Name : Erick
              Age : 30
              Sex : Male
          

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 1
          • Mark OlsonM
            Mark Olson
            last edited by

            All of the above solutions are good, but they are not necessarily very robust to reformatting of the document, which appears to be YAML.

            Normally this is where I’d recommend a plugin or a PythonScript solution for working with YAML, but I don’t know of any plugins, and Python’s YAML package is not in the Python standard library, so using PythonScript for YAML isn’t a great option.

            Having said that, my general suggestion for performing these kinds of manipulations on YAML documents is to learn a scripting language (I recommend Python) and using that to write normal Python scripts. If you end up going that route, this forum will cease to be a useful resource and StackOverflow becomes one of your best friends.

            1 Reply Last reply Reply Quote 2
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors