Community
    • Login

    Regular Expression - find and replace substring - further replacing all spaces in only defined substring with underscores

    Scheduled Pinned Locked Moved General Discussion
    3 Posts 3 Posters 1.0k Views 1 Watching
    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.
    • J S 1J Offline
      J S 1
      last edited by

      Hi Notepad++ Users,

      Could you please help me with the following search-and-replace problem I am having?

      I need to select a substring withing a string and replace all spaces with underscores

      Here is a sample of the data I currently have (“before” data):

      0000000001 001 0001 00000001 1 00000000 0000000000 Text with spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
      0000000001 001 0001 00000001 1 00000000 0000000000 Long variable length text with spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
      0000000001 001 0001 00000001 1 00000000 0000000000 A B C 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
      

      Here is how I would like that data to look (“after” data):

      0000000001 001 0001 00000001 1 00000000 0000000000 Text_with_spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
      0000000001 001 0001 00000001 1 00000000 0000000000 Long_variable_length_text_with_spaces 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
      0000000001 001 0001 00000001 1 00000000 0000000000 A_B_C 001 00000001 D 00000001 D 0001 01 00001 1* XX 00001 01
      

      NOTE:
      -the first part of each string ‘\1’ is fixed length <— (^13[ 0-9]{51}) finds the first part
      -the second part of the strings ‘\2’ are the parts that have whatever characters with spaces where spaces need to be replaced with “_” <— (*) finds this second part
      -the third part of the strings ‘\3’ have variable lengths and alphanumeric characters <— ( [0-9]{3} [0-9]{2,22} [DC] *^13) seems to do the trick

      To accomplish this, I have tried using the following Find/Replace expressions and settings

      • Find What = (^13[ 0-9]{51})(*)( [0-9]{3} [0-9]{2,22} [DC] *^13) <=== Using MS Word(###but I would like to use NotePad++###)
      • Replace With = ^13\2^13 <=== Using MS Word(###but I would like to use NotePad++###)
      • Search Mode = wildcards <=== Using MS Word(###but I would like to use NotePad++###)

      HERE IS WHY YOU THOUGHT YOUR EXPRESSION WOULD WORK

      This returns just the substring of interest (the text where spaces need to be replaced with underscores) - \1 and \3 are omitted in replace with

      Unfortunately, this does not produce the output I desired
      I need to further process part of the found string - ‘\2’ - replacing " " with “_”
      To get to what I need, I’d need to somehow use Replace with = Replace With = ^13\1{replace(\2," ","_")}\3^13 <=== curly brackets {} enclosing the needed replacement of substring ?

      Could you please help me understand what went wrong and help me find the solution?

      1 Reply Last reply Reply Quote 1
      • Alan KilbornA Offline
        Alan Kilborn
        last edited by

        If I were doing it, I’d try applying this technique: https://community.notepad-plus-plus.org/topic/22690

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

          Hello, @j-s-1,

          If you’re having trouble creating the right regular search expression, just ask me about it ;-)

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 1

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • First post
            Last post
          The Community of users of the Notepad++ text editor.
          Powered by NodeBB | Contributors