Community
    • Login

    Find&Replace pairs of capitalised words

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 376 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.
    • Joaquin BuenoJ
      Joaquin Bueno
      last edited by

      Hi everyone,

      I have tried finding what I need in the forum but I am missing part of the regex syntax. Hope you guys can help!

      I need to:
      Find: pairs of capitalised words separated by a space
      Replace: space for equal sign
      For example:
      Adam Sandler --> Adam=Sandler
      New Zealand --> New=Zealand

      With Regular expression checked, I have tried:

      [A-Z].*[A-Z]
      

      But that also brings up instances like:

      Christmas we're going to Lapland
      

      I would need something to limit the number of words that can fit in between [A-Z]s. I tried the following to limit the number of characters to 4 (as an example):

      [A-Z] .*\w{4} [A-Z]
      

      But it didn’t work either.

      I just need to find pairs of capitalised words but I am struggling.

      It would also be great if you could suggest a code that won’t spit out results of ‘I’ as in the pronoun. For example:

      Overwatch I'm in
      

      Thanks a lot in advance!

      EkopalypseE 1 Reply Last reply Reply Quote 1
      • EkopalypseE
        Ekopalypse @Joaquin Bueno
        last edited by

        @Joaquin-Bueno

        I assume this might work

        find what:([A-Z]\w+)(\h+)([A-Z]\w+)
        replace with:\1=\3
        make sure match case has been checked

        1 Reply Last reply Reply Quote 4
        • guy038G
          guy038
          last edited by

          Hello, @Joaquin Bueno, @ekopalypse and All,

          An alternate solution to @ekopalypse’s one could be :

          SEARCH (?-i)\u\w+\K\h+(?=\u\w*+(?!'))

          REPLACE =

          Notes :

          • First the (?-i) in-line modifier tells the regex engine to run the search in a NON-insensitive way

          • Then the part \u\w+ looks a word with a first upper-case letter followed with a non-null range of word characters

          • And the part \K resets the regex engine position of search and cancels the match, so far

          • Therefore, it searches, now, for a non-null range of horizontal blank characters \h+ ( Tabs and/or Spaces ) …

          • But ONLY IF  a double condition is true, due to the nested look-aheads structure :

            • It must be followed with an other word, of at least 1 upper-case letter, with possible subsequent word character(s), which…

            • Must not be followed with a single quote '. Thus, it eliminates cases of contractions such as “I’m, You’re, We’d,…”

          • In order that the negative look-ahead (?!') works as expected, we need to place the + regex symbol, after the part \w*, to set this group of word char(s) as an atomic group and, therefore, prevent the regex engine from backtracking inside it !

          • Note, also, that all text matched by the regexes, in look-arounds, are never part of the overall match

          • In replacement, the blank character(s) matched is/are replaced with one equal sign =

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 3
          • Joaquin BuenoJ
            Joaquin Bueno
            last edited by

            Thanks to both, @Ekopalypse and @guy038 :) :) :)

            Both codes worked like a charm.

            For anyone interested in these codes, @Ekopalypse 's code finds every space-separated pair of words starting with a capital letter , and @guy038 's code finds every space between words starting with a capital letter. Both were good for what I needed to do!

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