• Login
Community
  • Login

How to replace a dash with space within a word and not change after

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
7 Posts 2 Posters 3.2k 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.
  • K
    Kingfisher
    last edited by Kingfisher Aug 3, 2023, 3:09 PM Aug 3, 2023, 2:52 PM

    Have searched and just find the opposite.

    I am using replace files, have a series of files in a folder:

    Can look like this, some variants:

    ABB.ST,2023-08-01,421.2,423.6,416.8,416.8,505368 (No change)
    Result needed
    ABB,2023-08-01,421.2,423.6,416.8,416.8,505368

    ASSA-B.ST,2023-08-01,253,254.6,251.7,253.3,753197
    result needed
    ASSA B,2023-08-01,253,254.6,251.7,253.3,753197

    ACQ-SPAC.ST,2023-08-01,100.4,100.4,100,100.2,2119
    result needed
    ACQ SPAC,2023-08-01,100.4,100.4,100,100.2,2119

    Can it be done in one go or do I need to use alternates.

    for .ST .CO .HE .OL i have used
    (.ST\>) | (.CO\>) | (.HE\>)| (.OL\>) and replace with ()()()()

    Thanks in advance
    Best regards
    Stefan

    —

    moderator added code markdown around text; please don’t forget to use the </> button to mark example text as “code” so that characters don’t get changed by the forum

    P 1 Reply Last reply Aug 3, 2023, 3:10 PM Reply Quote 0
    • P
      PeterJones @Kingfisher
      last edited by PeterJones Aug 3, 2023, 3:12 PM Aug 3, 2023, 3:10 PM

      @Kingfisher ,

      I used moderator powers to edit your post so that we can better see/understand the data (please remember to format future posts – the backslash before your > got lost when you posted, as the PREVIEW window should have made obvious to you).

      But even after that, I’m not sure I’ve completely understood your needs, because your replacement seems to have nothing to do with your title. You originally said “how to replace a dash with a space within a word and not change after”, but what you demonstrated you wanted was “replace a hyphen with a space and then delete the period and some characters after as well”.

      For the sake of the answer, I am going to assume that

      1. You only want to do this search on the first “word” in a given line
      2. There are no leading spaces before the first “word” on a given line
      3. You want to look for a pattern like X-Y.Z, and replace it with X Y
      4. You don’t have any special needs as to the number of characters in each of the X and Y and Z positions.

      If these assumptions aren’t valid, then you need to show some more counter examples (using the recommended forum formatting) or I will not be able to provide any more suggestions.

      FIND = ^(\w+)-(\w+)\.(\w+)\>
      REPLACE = $1 $2
      SEARCH MODE = Regular Expression

      Explanation:

      • ^ = start at beginning of line
      • (\w+) = find 1 or more alphanumeric characters and put them in group#1
      • - = find a hyphen
      • (\w+) = find 1 or more alphanumeric characters and put them in group#2
      • \. = match a literal dot (in regex, . by itself means match any single character, so your .ST\> would have matched at the end of NOT_THE_BEST, – which I don’t think is what you wanted)
      • (\w+)\> put the word after the dot until the end of the word into group#3
      • Replacement just replaces with group#1 contents, then a literal space, then group#2 contents.

      Good luck.

      ----

      Useful References

      • Please Read Before Posting
      • Template for Search/Replace Questions
      • Formatting Forum Posts
      • Notepad++ Online User Manual: Searching/Regex
      • FAQ: Where to find other regular expressions (regex) documentation
      K 1 Reply Last reply Aug 3, 2023, 3:41 PM Reply Quote 1
      • K
        Kingfisher @PeterJones
        last edited by Aug 3, 2023, 3:41 PM

        @PeterJones
        Hi Peter,

        That worked fine for all the tricky ones. (-:

        undefined
        

        ABB.ST ,2023-08-01,421.2,423.6,416.8,416.8,505368 remained the same
        wanted result
        ABB,2023-08-01,421.2,423.6,416.8,416.8,505368 come in 3 variants but guess they will be handled
        by the same logic. Here no dash -

        BAVA.CO ,2023-08-01,421.2,423.6,416.8,416.8,505368
        ORNBV.HE,2023-08-01,421.2,423.6,416.8,416.8,505368
        KOG.OL,2023-08-01,421.2,423.6,416.8,416.8,505368

        undefined
        

        Thanks for your assistance.

        BR Stefan

        1 Reply Last reply Reply Quote 0
        • K
          Kingfisher
          last edited by Aug 3, 2023, 3:44 PM

          @PeterJones said in How to replace a dash with space within a word and not change after:

          or the sake of the answer, I am going to assume that

          You only want to do this search on the first “word” in a given line - Correct
          There are no leading spaces before the first “word” on a given line - No
          You want to look for a pattern like X-Y.Z, and replace it with X Y - Can be KOG.OL replace KOG or ALK-B.CO replace ALK B
          You don’t have any special needs as to the number of characters in each of the X and Y and Z positions. No it can be from 2- say 10

          P 1 Reply Last reply Aug 3, 2023, 4:20 PM Reply Quote 0
          • P
            PeterJones @Kingfisher
            last edited by Aug 3, 2023, 4:20 PM

            There are no leading spaces before the first “word” on a given line - No

            ^\h* instead of just ^ in the regex above will allow 0 or more spaces at the beginning of the line

            You want to look for a pattern like X-Y.Z, and replace it with X Y - Can be KOG.OL replace KOG or ALK-B.CO replace ALK B

            Since I explained all the pieces of the search regex, I am going to hope you are able to figure out how to delete the hyphen and the second group in the search, and remove the space and $2 from the replacement, so that you can run a second regex to catch those outliers. Because you need to learn by doing. And it’s a pretty easy simplification of the regex I already wrote for you. (it could be made more complicated, to handle both conditions in a single replacement, but it makes it harder to understand, and if two easier regex will do, I see no reason to combine them into one complicated regex)

            K 1 Reply Last reply Aug 3, 2023, 4:32 PM Reply Quote 0
            • K
              Kingfisher @PeterJones
              last edited by Aug 3, 2023, 4:32 PM

              @PeterJones said in How to replace a dash with space within a word and not change after:

              Can be KOG.OL replace KOG or ALK-B.CO replace ALK B

              No they can never replace in that direction.

              only from KOG.OL to KOG or ALK-B.CO replace ALK B

              Thanks I am learning.

              Best regards

              Stefan

              K 1 Reply Last reply Aug 3, 2023, 5:04 PM Reply Quote 0
              • K
                Kingfisher @Kingfisher
                last edited by Aug 3, 2023, 5:04 PM

                @Kingfisher
                Here is my solution ending up with 2 expressions

                undefined
                

                ^(\w+)-(\w+).(\w+)>
                $1 $2

                ^(\w+).(\w+)>
                $1$3

                undefined
                

                And then I bundble them in a macro with a hot key.

                Thanks for your help!

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