Community
    • Login

    Add space after specific character at the beginning of sentence.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    8 Posts 3 Posters 379 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.
    • J
      JajinderK
      last edited by JajinderK

      Hi there.

      I’m pretty new to the regex-thing, but that’s my own lazy-ass-fault. I’ve been trying to learn it a bit now (holy crap, is it overwhelming…), but I’m somewhat stuck. (see image)

      2024-11-19_141434.png

      After every “-” at the beginning of sentence, add a space between “-” and capital letter. (or replace “-” with "- ")

      These are the lines in the image:

      [INPUT]
      
      -Is that good? -That is gooder.
      -Scary good, but not gooder-good.
      -It was the goodest!
      
      RegEx used: (-)([A-Z])
      Replace with:  "\1 \2"
      
      [OUTPUT]
      
      - Is that good? - That is gooder.
      - Scary good, but not gooder- good.
      - It was the goodest!
      

      “gooder- good” should be “gooder-good”. No space here.

      My logic was: They have in common that every sentence starts with “-” + “A-Z”. I group “-” and “A-Z” and add a space between them.

      The regex (-)([A-Z]) works fine in regex101.com, see image
      (don’t understand why I get a pattern error in Substitution though)

      Where am I going wrong with this?

      Thanks for the help in advance.

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @JajinderK
        last edited by

        @JajinderK ,

        First off, Notepad++ uses the Boost regular expression library; regex101 does not; there may be slight differences in syntax between the two (though that’s not the cause of your problem, in this case).

        Your problem is that by default, regex101 uses case-sensitive matching, so [A-Z] only matches uppercase. However, you have not checkmarked ☐ Match case in the Notepad++ Replace dialog, so Notepad++ is ignoring case, and thus [A-Z] is actually matching [A-Za-z]. If you want the regex to pay attention to case, use ☑ Match case.

        J 1 Reply Last reply Reply Quote 3
        • J
          JajinderK @PeterJones
          last edited by

          @PeterJones

          Thank you for noticing that. It fixed my issue.

          @PeterJones said in Add space after specific character at the beginning of sentence.:

          @JajinderK ,

          First off, Notepad++ uses the Boost regular expression library; regex101 does not; there may be slight differences in syntax between the two.

          I just noticed it. Another software I’m using, uses .NET RegEx and for grouping you need to use “$1”, instead of “\1”. Things are confusing the hell out of me now. Whish it was a bit more universal. People changing things for the sake of “because I can” it seems like, but not making it easier.

          Anyways, thanks again.

          Alan KilbornA 2 Replies Last reply Reply Quote 2
          • Alan KilbornA
            Alan Kilborn @JajinderK
            last edited by Alan Kilborn

            @JajinderK said in Add space after specific character at the beginning of sentence.:

            Whish it was a bit more universal.

            Soooo many flavors of regex out there (at least this many):

            2bca242a-ba95-4be6-8c69-f7c48fd1ed3a-image.png

            J 1 Reply Last reply Reply Quote 1
            • Alan KilbornA
              Alan Kilborn @JajinderK
              last edited by Alan Kilborn

              @JajinderK said in Add space after specific character at the beginning of sentence.:

              you need to use “$1”, instead of “\1”.

              I actually prefer to use $1 in Notepad++ (note: it’s valid there) and other places as well.
              With \1 I start wondering, “do I need to double this backslash the way I’m using this?”. Using $1 there is no “escaping” concern. Hopefully you know what “escaping” means.

              In truth, I really prefer ${1} because if you need to do $1 followed by a literal 0, and you try doing $10, you don’t get what you want (you get group 10 instead of group 1). Use ${1}0 and you do get what you want.


              Things are confusing the hell out of me now.

              Oh…well…it’s just beginning, man. :-)

              1 Reply Last reply Reply Quote 4
              • J
                JajinderK @Alan Kilborn
                last edited by JajinderK

                @Alan-Kilborn said in Add space after specific character at the beginning of sentence.:

                Soooo many flavors of regex out there (at least this many):

                2bca242a-ba95-4be6-8c69-f7c48fd1ed3a-image.png

                That is sooooooo not funny.

                But you gave me great tips regarding the \1 and the ${1}. Thank you.

                It’s not only confusing me, it’s infuriating me actually, cause I really want to know how it works, but it’s not that simple if you don’t know the basics. For example the regex I made, is too simple. “What if the next character after the dash is a . (dot)” and I try to fix it with the limited knowledge what I have… uuuuugh. I’m struggling and I try to find examples, but I will get there eventually (I hope, if don’t give up … XD)

                EDIT: (-)(\.|[A-Z]) => Just learned that \. matches the dot XD

                PeterJonesP 1 Reply Last reply Reply Quote 0
                • PeterJonesP
                  PeterJones @JajinderK
                  last edited by

                  @JajinderK said in Add space after specific character at the beginning of sentence.:

                  EDIT: (-)(.|[A-Z]) => Just learned that \ . (without the space) matched the dot XD

                  Yes, \. matches the literal dot character.

                  Hint for the forum: put ` backticks around regex, because that makes sure that the forum software won’t change your text. So `(-)(\.|[A-Z])` will be rendered as (-)(\.|[A-Z]) in the forum.

                  (I see that continued edits, it’s getting closer to rendering like you want… good job)

                  The Template for Search/Replace FAQ, and the Formatting Forum Posts FAQ (linked below) explain more about how to format your posts for asking these kinds of questions.

                  And the URL for the User Manual’s regex section will be indispensable for someone trying to actually learn how Notepad++ regex work.

                  ----

                  Useful References

                  • Please Read Before Posting
                  • Template for Search/Replace Questions
                  • Formatting Forum Posts
                  • Notepad++ Online User Manual: Searching/Regex
                  J 1 Reply Last reply Reply Quote 1
                  • J
                    JajinderK @PeterJones
                    last edited by JajinderK

                    @PeterJones said in Add space after specific character at the beginning of sentence.:

                    @JajinderK said in Add space after specific character at the beginning of sentence.:

                    The Template for Search/Replace FAQ, and the Formatting Forum Posts FAQ (linked below) explain more about how to format your posts for asking these kinds of questions.

                    And the URL for the User Manual’s regex section will be indispensable for someone trying to actually learn how Notepad++ regex work.

                    Thank you so much for the links. (I hope I won’t give up.)

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