• Login
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.
  • A
    Andrew Schultz
    last edited by Andrew Schultz Dec 9, 2016, 4:17 PM Dec 9, 2016, 4:16 PM

    I have a few lines of text like so:

    nnseww
    wwwnensen

    Basically north, south, east and west. It’s not bad at all in PERL.

    $x =~ tr/nsew/snwe/;

    And I have a way to flip things around, but it’s messy.

    • change n to z1
    • change s to z2
    • change e to z3
    • change w to z4
    • change z1 to s
    • change z2 to n
    • change z3 to w
    • change z4 to e

    Now, this works, but it doesn’t feel as clean as, say, the perl script that translates…

    I also noticed this SuperUser thread. Has there been any new regex stuff in the last 4 years, or will I just need to use PERL for this sort of thing?

    This is hardly a painful issue but I was just curious about if there was a quicker way to do things than what I found. Thanks!

    C 1 Reply Last reply Dec 9, 2016, 5:21 PM Reply Quote 0
    • C
      Claudia Frank @Andrew Schultz
      last edited by Dec 9, 2016, 5:21 PM

      @Andrew-Schultz

      if by, any chance, a python script can be used, than this one line could be helpful.

      editor.replaceSel(''.join([{'n':'s','s':'n','e':'w','w':'e'}.get(x, x) for x in editor.getSelText()]))
      

      It gets a selected text and replaces each n,s,e,w with their flipped counterparts.

      Cheers
      Claudia

      1 Reply Last reply Reply Quote 2
      • G
        guy038
        last edited by guy038 Dec 9, 2016, 7:05 PM Dec 9, 2016, 7:04 PM

        Hello, Andrew Schultz and Claudia,

        This time, Claudia, my following regex S/R seems shorter than your Python script ;-))

        So, Andrew, just :

        • Select the Regular expression mode in the Replace dialog

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

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

        • Click on the Replace All button

        Et voilà !

        Best Regards,

        guy038

        C A G 3 Replies Last reply Dec 9, 2016, 7:10 PM Reply Quote 4
        • C
          Claudia Frank @guy038
          last edited by Dec 9, 2016, 7:10 PM

          @guy038
          what should I say - brilliant :-)
          Maybe we should think about rejecting all programming languages and do regex only ;-)

          Cheers
          Claudia

          1 Reply Last reply Reply Quote 0
          • J
            Jim Dailey
            last edited by Dec 9, 2016, 7:32 PM

            @Claudia-Frank Until you realize that regex is just another programming language. :-)

            C 1 Reply Last reply Dec 9, 2016, 7:37 PM Reply Quote 0
            • C
              Claudia Frank @Jim Dailey
              last edited by Dec 9, 2016, 7:37 PM

              @Jim-Dailey

              a vicious circle :-D

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              • G
                guy038
                last edited by guy038 Dec 9, 2016, 9:24 PM Dec 9, 2016, 9:22 PM

                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

                S 1 Reply Last reply Dec 21, 2016, 9:23 PM Reply Quote 2
                • A
                  Andrew Schultz @guy038
                  last edited by Dec 13, 2016, 1:18 PM

                  @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
                  • S
                    Scott Sumner
                    last edited by Dec 17, 2016, 9:19 PM

                    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
                    
                    C 1 Reply Last reply Dec 17, 2016, 9:26 PM Reply Quote 2
                    • C
                      Claudia Frank @Scott Sumner
                      last edited by Dec 17, 2016, 9:26 PM

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

                      Cheers
                      Claudia

                      S 1 Reply Last reply Dec 17, 2016, 9:29 PM Reply Quote 0
                      • S
                        Scott Sumner @Claudia Frank
                        last edited by Dec 17, 2016, 9:29 PM

                        @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
                        • S
                          Scott Sumner @guy038
                          last edited by Dec 21, 2016, 9:23 PM

                          @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…

                          C 1 Reply Last reply Dec 21, 2016, 11:34 PM Reply Quote 0
                          • C
                            Claudia Frank @Scott Sumner
                            last edited by Dec 21, 2016, 11:34 PM

                            @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

                            S 1 Reply Last reply Dec 21, 2016, 11:46 PM Reply Quote 0
                            • S
                              Scott Sumner @Claudia Frank
                              last edited by Dec 21, 2016, 11:46 PM

                              @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
                              • G
                                glennfromiowa @guy038
                                last edited by Feb 24, 2017, 3:51 AM

                                @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!

                                S 1 Reply Last reply Feb 24, 2017, 7:20 PM Reply Quote 0
                                • S
                                  Scott Sumner @glennfromiowa
                                  last edited by Feb 24, 2017, 7:20 PM

                                  @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
                                  • G
                                    glennfromiowa
                                    last edited by Feb 24, 2017, 8:35 PM

                                    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