• Login
Community
  • Login

Find and Replace Colon Between Numbers

Scheduled Pinned Locked Moved General Discussion
4 Posts 3 Posters 1.9k 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.
  • Y
    Yohanes K
    last edited by Nov 15, 2019, 5:20 PM

    Hello all,

    I would like to do find and replace:

    1. Colon between numbers
    2. Dash between numbers

    Original:
    Matthew 22:23-33

    Become:
    Matthew 22 verse 23 to 33

    But it does not have impact to other word if it is not between numbers, example= God:

    Please let me know, thank you in advance.

    1 Reply Last reply Reply Quote 0
    • P
      PeterJones
      last edited by PeterJones Nov 15, 2019, 9:23 PM Nov 15, 2019, 9:23 PM

      @Yohanes-K,

      Matthew 22:23-33
      
      God said: "Let-there-be-light"
      
      Score 15-17
      
      Gen 1:1
      
      Boring: text
      

      becomes

      Matthew 22 verse 23 to 33
      
      God said: "Let-there-be-light"
      
      Score 15 to 17
      
      Gen 1 verse 1
      
      Boring: text
      

      using

      • search mode = regular expression
      • find = \d+\K(?:(:)|(-))(?=\d)
      • replace = (?1 verse )(?2 to ) – yes, there are spaces around “verse” and “to” in that expression; copy-paste should work right

      Quick explanation:

      • find one or more digits,
      • reset the search, so those digits aren’t included in the “match text”
      • look for either : (which will be called group 1) or - (which will be called group 2)
      • followed by another digit (but don’t keep this digit in the match, either
      • replace group 1 (if any : matched) with " verse " (including spaces)
      • replace group 2 (if any - matched) with " to " (including spaces)

      Notice my example used counter-examples which don’t match, but might mistakenly do so with a less-than-ideal regex; this makes it much more clear whether the expression is working, or just seems to be working on the positive-match data.

      -----

      Please Read And Understand This

      FYI: I often add this to my response in regex threads, unless I am sure the original poster has seen it before. Here is some helpful information for finding out more about regular expressions, and for formatting posts in this forum (especially quoting data) so that we can fully understand what you’re trying to ask:

      This forum is formatted using Markdown . Fortunately, it has a formatting toolbar above the edit window, and a preview window to the right; make use of those. The </> button formats text as “code”, so that the text you format with that button will come through literally; use that formatting for example text that you want to make sure comes through literally, no matter what characters you use in the text (otherwise, the forum might interpret your example text as Markdown, with unexpected-for-you results, giving us a bad indication of what your data really is). Images can be pasted directly into your post, or you can hit the image button. (For more about how to manually use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic , and my updates near the end .) Please use the preview window on the right to confirm that your text looks right before hitting SUBMIT. If you want to clearly communicate your text data to us, you need to properly format it.

      If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study the official Notepad++ searching using regular-expressions docs , as well as this forum’s FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.

      Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.

      1 Reply Last reply Reply Quote 2
      • G
        guy038
        last edited by Nov 16, 2019, 10:17 AM

        Hi, @yohanes-k, @peterjones, and All,

        Peter, you could, even, have omitted the non-capturing group, giving :

        SEARCH \d+\K(:)|-(?=\d+)

        REPLACE \x20(?1verse:to)\x20

        Be aware, @yohanes-k, to click, exclusively, on the Replace All button ( Due to the \K syntax, the step by step Replace button does not work properly ! )

        Cheers,

        guy038

        1 Reply Last reply Reply Quote 2
        • G
          guy038
          last edited by guy038 Nov 16, 2019, 4:08 PM Nov 16, 2019, 4:07 PM

          Hello, @yohanes-k, @peterjones, @ekopalypse and All,

          Oh, my bad ! Reading again my post, I just realize that I’m quite wrong and that the Peter’s solution is the right one !

          Why ? because my search regex, in fact, is an alternative between the 2 independent regexes : \d+\K(:) and -(?=\d+) :-((

          Actually, the overall regex must be an alternative between the regexes \d+\K:(?=\d+) and \d+\K-(?=\d+) which may be grouped with a ( non-capturing ) group syntax !


          Now, in order to be more rigorous, we should mention the name of the Four Evangelists. This gives the following regex S/R :

          SEARCH (?-i:Matthew|Mark|Luke|John)\x20\d+\K:(\d+)-(?=\d+)

          REPLACE \x20verse\x20\1\x20to\x20

          Cheers,

          guy038

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