• Login
Community
  • Login

Need help on replacing text

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
13 Posts 2 Posters 3.4k 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.
  • C
    Claude Cadieux
    last edited by Mar 24, 2020, 5:50 PM

    Hi all,
    I need help in replacing text in a Gedcom file (Genealogy Export).
    These are examples of lines in my file.
    The first line, I must not touch, only has 1 date.
    The second line, I must change ABT to BET
    The problem that I have is that the years are variable.
    I need to detect that 2 years are stated and separated by a “-” following “ABT”
    Then change “ABT” by “BET” leaving the dates untouched.

    2 DATE ABT 1646
    2 DATE ABT 1676-1678

    Can anyone help with some kind of “Chage” using Regex or any othe kind of change?

    Thanks
    Claude

    A 1 Reply Last reply Mar 24, 2020, 5:56 PM Reply Quote 0
    • A
      Alan Kilborn @Claude Cadieux
      last edited by Alan Kilborn Mar 24, 2020, 5:57 PM Mar 24, 2020, 5:56 PM

      @Claude-Cadieux

      How about:

      Find what box: ABT(?= \d{4}-\d{4})
      Replace with box: BET
      Match case checkbox: ticked
      Wrap around checkbox: ticked
      Search mode radiobutton: Regular expression
      Press the Replace All button

      Or maybe you also need to key off of start of line, and include the 2 DATE stuff as well? Hard to say from your problem statement.

      C 1 Reply Last reply Mar 24, 2020, 7:13 PM Reply Quote 1
      • C
        Claude Cadieux
        last edited by Mar 24, 2020, 6:07 PM

        @Alan-Kilborn said in Need help on replacing text:

        ABT(?= \d{4}-\d{4})

        Worked perfectly. Thank you very much.

        This topic may be closed.

        Claude

        1 Reply Last reply Reply Quote 2
        • C
          Claude Cadieux @Alan Kilborn
          last edited by Mar 24, 2020, 7:13 PM

          @Alan-Kilborn said in Need help on replacing text:

          ABT(?= \d{4}-\d{4})

          Sorry to ask another question about this.
          The change worked from ABT to BET but I must make another change if you can spare a moment.
          I need to change this line:
          2 DATE BET 1676-1678

          to this:
          2 DATE BET 1676 AND 1678

          Replacing the “-” between the 2 dates by " AND "

          Thanks again.
          Claude

          A 1 Reply Last reply Mar 24, 2020, 7:18 PM Reply Quote 0
          • A
            Alan Kilborn @Claude Cadieux
            last edited by Alan Kilborn Mar 24, 2020, 7:18 PM Mar 24, 2020, 7:18 PM

            @Claude-Cadieux

            So given our first exchange, might you see which part of that pattern I provided matches four-digit years? Go ahead, take a guess! We’ll be kind.

            While we do like providing help, we really enjoy it when people take the help provided and apply it to new problems. Makes us happy.

            C 1 Reply Last reply Mar 24, 2020, 7:26 PM Reply Quote 2
            • C
              Claude Cadieux @Alan Kilborn
              last edited by Mar 24, 2020, 7:26 PM

              @Alan-Kilborn
              I did look at your example and tried some variant but they did not work. Then went to look at Regex example online but man, at 65 I must say that I am a bit overwhelmed.
              I tried this:
              -(?= \d{4}-\d{4})

              I tried this:
              -(?= \d{4}-\d{4})

              Can’t get a match and also, I need to insert a blank before and after “AND”.
              \bAND\b ??

              Thanks

              C 1 Reply Last reply Mar 24, 2020, 7:27 PM Reply Quote 0
              • C
                Claude Cadieux @Claude Cadieux
                last edited by Mar 24, 2020, 7:27 PM

                @Claude-Cadieux said in Need help on replacing text:

                -(?= \d{4}-\d{4})

                Sorry on the second try I did this:
                -(?= \d{4}-\d{4})

                C A 2 Replies Last reply Mar 24, 2020, 7:28 PM Reply Quote 0
                • C
                  Claude Cadieux @Claude Cadieux
                  last edited by Mar 24, 2020, 7:28 PM

                  @Claude-Cadieux
                  Backslash does not show here \-(?= \d{4}-\d{4})

                  1 Reply Last reply Reply Quote 0
                  • A
                    Alan Kilborn @Claude Cadieux
                    last edited by Mar 24, 2020, 7:30 PM

                    @Claude-Cadieux

                    So to match a 4-digit year you can use \d{4}. That means “a digit character repeated 4 times”.

                    A blank is not \b. A blank space is just a blank space in your expression.

                    \b is actually a word-boundary.

                    It is best to show expressions surrounded by backticks; they will appear like mine do, in red.

                    A 1 Reply Last reply Mar 24, 2020, 7:34 PM Reply Quote 1
                    • A
                      Alan Kilborn @Alan Kilborn
                      last edited by Alan Kilborn Mar 24, 2020, 7:36 PM Mar 24, 2020, 7:34 PM

                      @Claude-Cadieux

                      went to look at Regex example online but man, at 65 I must say that I am a bit overwhelmed

                      I would not believe you if you said anything different. :-)

                      So by now you should be matching 1968-1974 as \d{4}-\d{4} ??

                      Admittedly, my earlier stuff with (?= before it might have been misleading. Forget about that now (advanced usage).

                      C 1 Reply Last reply Mar 24, 2020, 7:42 PM Reply Quote 0
                      • C
                        Claude Cadieux @Alan Kilborn
                        last edited by Mar 24, 2020, 7:42 PM

                        @Alan-Kilborn
                        Yes this did work: \d{4}-\d{4}
                        But it must be on a BET line:
                        2 DATE BET 1625-1629
                        So this also works to find the right line
                        BET(?= \d{4}-\d{4})

                        But I do not know how to specify the “-” being the target to change by " AND "

                        A 1 Reply Last reply Mar 24, 2020, 8:03 PM Reply Quote 0
                        • A
                          Alan Kilborn @Claude Cadieux
                          last edited by Alan Kilborn Mar 24, 2020, 8:04 PM Mar 24, 2020, 8:03 PM

                          @Claude-Cadieux

                          So there may be something to learn here. With BET(?= \d{4}-\d{4}) (you didn’t apply my “backtick” advice when you stated it), you can’t possibly influence the - because the (?=...) part of that doesn’t match any text, it just means that the specified text must be there. If you don’t match the text you want, you can’t replace it. (Remember, originally, all we wanted to replace was some fixed text before the (variable) year range).

                          You might have good luck searching for BET (\d{4})-(\d{4}) and replacing with BET \1 AND \2.
                          You put grouping parentheses around variable text that you want to substitute in at replace time, as I’ve done here. Then, to refer to the first capturing group, at replace time you reference it with \1. Similar for the second grouping.

                          But really, this isn’t a forum about regular expressions, so time to buck up and learn a bit more than just your immediate need covers. There’s some good pointers here: https://community.notepad-plus-plus.org/topic/15765/faq-desk-where-to-find-regex-documentation

                          C 1 Reply Last reply Mar 24, 2020, 8:11 PM Reply Quote 2
                          • C
                            Claude Cadieux @Alan Kilborn
                            last edited by Mar 24, 2020, 8:11 PM

                            @Alan-Kilborn said in Need help on replacing text:

                            BET \1 AND \2

                            Thank you so much for your time and patience and the link.
                            There’s always something new to learn.
                            Claude :-)

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