Community
    • Login

    switching letters with one regex

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    regexsearch
    17 Posts 6 Posters 7.5k 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.
    • Claudia FrankC
      Claudia Frank @Jim Dailey
      last edited by

      @Jim-Dailey

      a vicious circle :-D

      Cheers
      Claudia

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

        Claudia and Jim,

        Claudia, oh no ! Definitively not ! It’s just that, when I see some changes of text, which involve current file ONLY, and which need ONLY one S/R, regular expressions seem to be, most of the time, the shorter way to get the job done !


        BTW, I will send you, soon, my own version of your excellent RegexTesterPro.py script. Just to know your feedback about my modifications and, ( again ! ) some suggestions :-))

        Cheers,

        guy038

        P. S. :

        BTW, for an quick oversight about the differences between Formal languages, Programming languages and Regular expressions, refer to that interesting article ( especially, the answers of babou and tsleyson !! )

        http://cs.stackexchange.com/questions/30639/what-is-the-relationship-between-programming-languages-regular-expressions-and


        Specifically, about regular expressions, read this Wikipedia article :

        https://en.wikipedia.org/wiki/Regular_expression#

        And, particularly, the sections :

        https://en.wikipedia.org/wiki/Regular_expression#Formal_language_theory

        https://en.wikipedia.org/wiki/Regular_expression#Patterns_for_non-regular_languages

        Scott SumnerS 1 Reply Last reply Reply Quote 2
        • Andrew SchultzA
          Andrew Schultz @guy038
          last edited by

          @guy038 Wow! Very well done and creative! Thanks also for the other links.

          @Claudia-Frank I’m learning python, and a script like that helps show me its power. This looks like it can be extended nicely to other cases where I need to flip 2 strings or rotate 3. So I really appreciate that.

          I wish I’d remembered to check earlier…I need to go figure how to send alerts to my email. I thought it would’ve happened automatically. It’s pretty awesome, though, to learn about programming on a word processing forum. NotePad++ really has been a boon to me.

          1 Reply Last reply Reply Quote 0
          • Scott SumnerS
            Scott Sumner
            last edited by

            So, I, too, have a Perl background and have more than once, when doing Python, wished for something like Perl’s tr feature. So this thread inspired me to put together something a little more generic than @Claudia-Frank 's earlier script. The following can be used in either Python or Notepad++'s Pythonscript:

            def translate(input_str, orig_chars, new_chars):
                assert len(orig_chars) <= len(new_chars)
                trans_dict = {}
                for (j, v) in enumerate(orig_chars): trans_dict[v] = new_chars[j]
                return ''.join([ trans_dict.get(x, x) for x in input_str ])
            

            An example calling, using this thread’s original problem text, would be:

            x = 'nnswwennswwwwweee'
            print x
            y = translate(x, 'news', 'swen')
            print y
            

            which will print:

            nnswwennswwwwweee
            ssneewssneeeeewww
            
            Claudia FrankC 1 Reply Last reply Reply Quote 2
            • Claudia FrankC
              Claudia Frank @Scott Sumner
              last edited by

              Scott,
              a nice one - like it.
              Copied and backed up.

              Cheers
              Claudia

              Scott SumnerS 1 Reply Last reply Reply Quote 0
              • Scott SumnerS
                Scott Sumner @Claudia Frank
                last edited by

                @Claudia-Frank

                HAHA…well, it’s pretty much an obvious rip-off of your earlier one, which hurt my brain when I first saw it. :-)

                1 Reply Last reply Reply Quote 1
                • Scott SumnerS
                  Scott Sumner @guy038
                  last edited by

                  @guy038 said:

                  my own version of your excellent RegexTesterPro.py script

                  Hello guy,

                  I was just wondering if we are going to see your mentioned script HERE, perhaps sometime soon? :)

                  One thing that concerns me is that, while I’m sure the script is super-useful (as @Claudia-Frank 's versions in the past have also been), it will do Python-flavored regex’s and not Notepad++ -flavored regexes. For example, I have tried the “news” substitution that this thread began with, and while it works in N++, I haven’t been able to get something like it to work in Python–maybe I’m doing something wrong…

                  Claudia FrankC 1 Reply Last reply Reply Quote 0
                  • Claudia FrankC
                    Claudia Frank @Scott Sumner
                    last edited by

                    @Scott-Sumner

                    editor.rereplace(r'(s)|(n)|(e)|(w)', r'(?1n)(?2s)(?3w)(?4e)')
                    

                    did I miss something?
                    Or did you use the python re module?
                    In this case, yes, it doesn’t have the conditional substituion functionality.

                    Just for completeness, RegexTester has been designed to test a regular expression,
                    current version, and I assume this is still true even with the changes guy made,
                    cannot do replaces.

                    Cheers
                    Claudia

                    Scott SumnerS 1 Reply Last reply Reply Quote 0
                    • Scott SumnerS
                      Scott Sumner @Claudia Frank
                      last edited by

                      @Claudia-Frank

                      I was speaking of the Python re module; I forgot about the .research and .rereplace functions as I just tend to use the re ones. It didn’t occur to me that the editor ones would call the same regex engine as an interactive find/replace, but it makes perfect sense that they would. Thanks (again) for pointing out the obvious.

                      I realize the regextester currently does only find, but was thinking that the next logical extension might be to do something with replace. Thinking more about it, maybe that isn’t really practical…

                      1 Reply Last reply Reply Quote 0
                      • glennfromiowaG
                        glennfromiowa @guy038
                        last edited by

                        @guy038 said:

                        • Type, in the Find what: zone, (s)|(n)|(e)|(w)

                        • Type, in the Replace with: zone, (?1n)(?2s)(?3w)(?4e)

                        I just stumbled across your post, and this is the first time I’ve ever seen this! Where is this documented? I mean, I think your example tells me everything I need to know, but I wish there were more examples like this in the Wiki page on Regular Expressions! It isn’t even mentioned that the (?...) construct can be used in the Replace with (Substitutions) part. I felt like it should be able to be done, and yet I hadn’t been able to figure out the syntax yet. So powerful, and yet way too much trial and error needed to discover what it can do!

                        Scott SumnerS 1 Reply Last reply Reply Quote 0
                        • Scott SumnerS
                          Scott Sumner @glennfromiowa
                          last edited by

                          @glennfromiowa

                          You can find that in the Boost Regex Replace documentation, in the “Conditionals” section. Here’s a possible link to that Boost documentation: http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html

                          The example from @guy038 's usage is a special case of this explanation from those docs:

                          For example, the format string "(?1foo:bar)" will replace each match found with "foo" if the sub-expression $1 was matched, and with "bar" otherwise.

                          BTW, @glennfromiowa , Welcome Back to the Community after a long time away…where ya been? :-D

                          1 Reply Last reply Reply Quote 0
                          • glennfromiowaG
                            glennfromiowa
                            last edited by

                            Thanks, @Scott Sumner, I’ve been workin’. But now my work seems to lie in using RegExes to do things I couldn’t do easily without them, so here I am, back again.

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