switching letters with one regex



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



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



  • 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



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

    Cheers
    Claudia



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



  • @Jim-Dailey

    a vicious circle :-D

    Cheers
    Claudia



  • 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



  • @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.



  • 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


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

    Cheers
    Claudia



  • @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. :-)



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



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



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



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



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



  • 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.


Log in to reply