Sublime Forum

RegReplace Plugin

#55

Hi, I hope ‘facelessuser’ can answer me this:

i’m trying to use this snippet:

// busca consultas SQL y las encierra en query("consulta") "wrap_sql_in_query": { "scope": "sql", "find" : "((\"|')(delete|select|insert|update)\\s+\\w\\s=,.'\\$;\\(\\)+-<>|]+(\"|'))", "replace": "query(\\1)", "greedy_replace": true, "case" : false },

I have that in /home/user/.config/sublime-text-2/Packages/RegReplace/reg_replace.sublime-settings

in /home/user/.config/sublime-text-2/Packages/RegReplace/Default.sublime-commands

i have:

{ "caption": "Reg Replace: Update PG to ADODB", "command": "reg_replace", "args": {"replacements": "wrap_sql_in_query", "update_pg_to_adodb", "update_pg_to_adodb1", "update_pg_result", "comment_pg_exec"], "find_only": true} },

but no results are highlighted, i tested the regex in both pythex.org/ and it worked fine (removing the escape )

the test case is:

$consulta    = "select consdocdeu,fechacompro from compromi where estado ='A' and fechacompro <= date(now()) -1  ";

and the expected result is:

$consulta    = query("select consdocdeu,fechacompro from compromi where estado ='A' and fechacompro <= date(now()) -1  ");

is like the backslashes aren’t working, but if i remove the backslashes sublime tells:

thank you!

0 Likes

#56

What language (syntax) is your code in?

0 Likes

#57

I threw this in PHP real quick.

Notice I removed the “sql” scope, because at least in PHP, the string did not scope to “sql”.

[pre=#232628] // busca consultas SQL y las encierra en query(“consulta”)
“wrap_sql_in_query”: {
// “scope”: “sql”,
“find” : “((”|’)(delete|select|insert|update)\s+\w\s=,.’\$;\(\)±<>|]+("|’))",
“replace”: “query(\1)”,
“greedy_replace”: true,
“case” : false
},[/pre]

You didn’t provide me with the other commands, so I reformatted the command to only call the wrap command you gave me.

[pre=#232628] {
“caption”: “Reg Replace: Wrap SQL Query”,
“command”: “reg_replace”,
“args”: {“replacements”: “wrap_sql_in_query”], “find_only”: true}
},[/pre]

It turned this:

[pre=#232628]<?

$consulta = "select consdocdeu,fechacompro from compromi where estado =‘A’ and fechacompro <= date(now()) -1 ";

?>[/pre]

To this just fine:

[pre=#232628]<?

$consulta = query("select consdocdeu,fechacompro from compromi where estado =‘A’ and fechacompro <= date(now()) -1 ");

?>[/pre]

Maybe removing the scope is all you need to do? Maybe there is something wrong with the additional commands you did not provide me with? I don’t know since you did not provide me enough information. But there is nothing wrong with the regex, and since I don’t know what syntax language you are using, I cannot be sure whether the sql scope requirement is helpful or not, but it is a hindrance in stock PHP.

0 Likes

#58

is php

0 Likes

#59

Removing the scope did the trick!,
thank you and excellent plugin, i though scope mean ‘group these regex’ its the scope mean for file types??

thank you again

[quote=“facelessuser”]I threw this in PHP real quick.

Notice I removed the “sql” scope, because at least in PHP, the string did not scope to “sql”.

[pre=#232628] // busca consultas SQL y las encierra en query(“consulta”)
“wrap_sql_in_query”: {
// “scope”: “sql”,
“find” : “((”|’)(delete|select|insert|update)\s+\w\s=,.’\$;\(\)±<>|]+("|’))",
“replace”: “query(\1)”,
“greedy_replace”: true,
“case” : false
},[/pre]

You didn’t provide me with the other commands, so I reformatted the command to only call the wrap command you gave me.

[pre=#232628] {
“caption”: “Reg Replace: Wrap SQL Query”,
“command”: “reg_replace”,
“args”: {“replacements”: “wrap_sql_in_query”], “find_only”: true}
},[/pre]

It turned this:

[pre=#232628]<?

$consulta = "select consdocdeu,fechacompro from compromi where estado =‘A’ and fechacompro <= date(now()) -1 ";

?>[/pre]

To this just fine:

[pre=#232628]<?

$consulta = query("select consdocdeu,fechacompro from compromi where estado =‘A’ and fechacompro <= date(now()) -1 ");

?>[/pre]

Maybe removing the scope is all you need to do? Maybe there is something wrong with the additional commands you did not provide me with? I don’t know since you did not provide me enough information. But there is nothing wrong with the regex, and since I don’t know what syntax language you are using, I cannot be sure whether the sql scope requirement is helpful or not, but it is a hindrance in stock PHP.[/quote]

0 Likes

#60

It applies to the highlight scoping. For instance, if you want the regex to only be applied to “strings”, you set the scope as “string”. Then RegReplace finds all of the parts of your file that are of the “string” scope, and applies the regex to them individually.

Like if I want to target strings for changing their quote style:

[pre=#232628] // Swap quotes
“swap_quotes_to_single”: {
“scope”: “string”,
“find” : “^”(.?)"$",
“replace”: “’\1’”,
“greedy_replace”: false
},
“escape_single_quotes”: {
“scope”: “string”,
“find” : “^(”.
?(?<!\\))((?:\\]{2}))’(.?")$",
“replace”: “\1\2\’\3”,
“greedy_replace”: false,
“multi_pass_regex”: true
},
“unescape_double_quotes”: {
“scope”: “string”,
“find” : “^(’.?(?<!\\))((?:\\]{2}))\\”(.*?’)$",
“replace”: “\1\2”\3",
“greedy_replace”: false,
“multi_pass_regex”: true
},[/pre]

[pre=#232628] // Flip Quotes
{
“caption”: “Replace: Swap Quotes to Single”,
“command”: “reg_replace”,
“args”: {“replacements”: “escape_single_quotes”, “swap_quotes_to_single”, “unescape_double_quotes”], “find_only”: true}
},[/pre]

etc.

Hope that helps.

0 Likes

#61

Oh I see, will be usefull for a case I have! thanks, keep the good work!

0 Likes

#62

ST3 only (currently).

RegReplace now allows you to do procedural evaluations on found text via plugins.

// Some defintion "some_definition": { "find": "some regex here", "replace": "some optional replace here", "greedy": true, "case": true, "plugin": "User.rr_modules.force_all_caps", "args": {"some_plugin_rguments": "if_desired"} },

A plugin with path “User/rr_modules/force_all_caps.py” would be specified in your defintion as “User.rr_modules.force_all_caps”.

Built-in RegReplace modules are simply referenced as “rr_modules.my_module” etc. (though there aren’t any provided at this time).

Plugins are very simple:

def run(text, **optional_keyword_arguments): # Do some stuff to text return text

That is it.

You can import RegReplace plugins into other RegReplace plugins if desired by.

[code]from RegReplace.rr_plugin import Plugin

def run(text, **optional_keyword_arguments):
module = Plugin.load(“User.rr_modules.myplugin”)
text = module.run(text)
return text[/code]

If I were creating a User module, I personally would include from a sub-folder in User that does not provide a “init.py”. This just basically means sublime won’t automatically import the plugin file.

I guess you could consider this feature in beta. So, real world feedback may cause some aspects to change in the future, but maybe not.

0 Likes

#63

Got some big changes.

[size=150]Extended Backreferences (Replace Only)[/size]

People occasionally complain that RegReplace can’t use title case back references:

[pre=#232628] “test_case”: {
“find”: “([a-z])(?P[a-z]*)((?:_[a-z]+)+)”,
“replace”: “\u\1\L\g\E\U\g<3>\E”,
“greedy”: true
}[/pre]

Well this is because the regex engine that RegReplace uses is not Sublime’s built in regex engine, but instead uses Python. I won’t go into reasons why, but suffice it to say, I have reasons. Now Python doesn’t support title case backreferences in its regex engine. So I have been thinking about this recently and added some extended back references to handle this.

Now hold up, it isn’t exactly the same! Python strings already use \u, so \u wasn’t an option. So it is a sightly altered notation:

[pre=#232628] “test_case”: {
“find”: “([a-z])(?P[a-z]*)((?:_[a-z]+)+)”,
“replace”: “\c\1\L\g\E\C\g<3>\E”,
“greedy”: true
},[/pre]

See the difference? You have to use \c and \C instead of \u and \U. Now this is disabled by default and only works for the replace string (I’m not re-writing the regex parser for matches), so you have to enable it in your settings file by using:

[pre=#232628] // Use extended backreferences
“extended_back_references”: true[/pre]

There might be more in the future.

[size=150]Updated Plugin System (More Useful)[/size]
Also, I recently realized the plugin system was not that good in its current state. So I simplified it.

You still define a plugin the same way (plugins are referenced like importing a python module with Packages as your root) User.rr_modules.:
[pre=#232628] “some_definition”: {
“find”: “some regex here”,
“replace”: “some optional replace here”,
“greedy”: true,
“case”: true,
“plugin”: “User.rr_modules.useful_stuff”,
“args”: {“some_plugin_rguments”: “if_desired”}
},[/pre]

And I have provided a simple example on the repo you can try out. Now you just define a function called replace and it accepts a match object and optional arguments (I show them in the example just to remind you you can use them):
[pre=#232628]def replace(m, **kwargs):
text = "Here are your groups: "
for group in m.groups():
if group is not None:
text += “(%s)” % group
return text[/pre]

0 Likes

#64

Edit: Posted in wrong thread.

0 Likes

#65

First of all let me say that i love this package, makes my life incredibly easier. :smiley:
A few days ago all my “reg_replace” commands disappeared from the command palette and key bindings stopped working as well.
I did some troubleshooting but nothing seemed to help, eventually i installed a fresh copy of Sublime Text and the RegReplace package in a different machine only to find that not even the examples in the Default.sublime-commands seem to be working anymore.
On a fresh install, tried adding this command to the list

{
    "caption": "Reg Replace: Remove Trailing Spaces",
    "command": "reg_replace",
    "args": {"replacements": "remove_trailing_spaces"]}
},

but is does not show up in the command palette.
"open_file " commands seem to work fine, only the “reg_replace” ones are giving me problems.
Is anyone having the same problem ? Is there anything else i should check ?

0 Likes

#66
  1. Github repo’s issue section is probably more appropriate for getting help as it actually notifies me and keeps a record in a place people can more easily find if they have similar issues.
  2. ST2 or ST3?
  3. Are you getting console errors? If you see no commands, usually it is because sublime failed to load the plugin. So search your console for errors you can share here.

RegReplace is working for me on ST3 OSX and Windows.

0 Likes

#67
  1. Done, moved to github. github.com/facelessuser/RegReplace/issues/29
  2. ST2
  3. Now that you mention the console, i do see an error on load there

Reloading plugin C:\Users\ricardosz\AppData\Roaming\Sublime Text 2\Packages\RegReplace\rr_extended.py
Reloading plugin C:\Users\ricardosz\AppData\Roaming\Sublime Text 2\Packages\RegReplace\rr_notify.py
Reloading plugin C:\Users\ricardosz\AppData\Roaming\Sublime Text 2\Packages\RegReplace\rr_plugin.py
Reloading plugin C:\Users\ricardosz\AppData\Roaming\Sublime Text 2\Packages\RegReplace\rr_replacer.py
Traceback (most recent call last):
File “.\sublime_plugin.py”, line 62, in reload_plugin
File “.\rr_replacer.py”, line 3, in
from RegReplace.rr_plugin import Plugin
ImportError: No module named RegReplace.rr_plugin
Reloading plugin C:\Users\ricardosz\AppData\Roaming\Sublime Text 2\Packages\RegReplace\rr_sequencer.py
Traceback (most recent call last):
File “.\sublime_plugin.py”, line 62, in reload_plugin
File “.\rr_sequencer.py”, line 11, in
from RegReplace.rr_replacer import FindReplace
ImportError: No module named RegReplace.rr_replacer

0 Likes

#68

There was an ST3 fix backported to ST2, but I mistakenly released the ST2 release from the wrong branch. Release 1.10.1 for ST2 should fix this issue when available in Package Control.

0 Likes

#69

I’m trying to replace few characters in my text, for example “š” with “\v{s}” but it doesn’t work :confused:

  • for replace with “\v{s}” i get error: Error trying to parse settings: Invalid escape in Packages/RegReplace/reg_replace.sublime-settings:87:24
  • for replace with “\v{s}” it just changes to: ‘vertical tab sign’{s}

can someone please explain how to keep \v?

0 Likes

#70

\v is not a recognized back reference. Since you are suing Python’s re regex module, you need to use something that works with that, so here is a simple example:

"unicode_test": { "find": "replace me", "replace": "<verttab>\u000B<verttab>" }

Good luck!

0 Likes

#71

2.2.0 is out.

  • Adds a number of improvements to handling upper case and lower case back references in replace patterns.
  • It also adds support for escaping characters with \Q…\E in search patterns.
  • Adds upper\lower case character classes via \l and \c in search or the inverse via \L and \C (if re.Unicode flag is enabled, they will act as unicode character classes).
  • Also adds support for unicode properties via \p{Letter_Number} or \p{Nl} or the inverse form \P{Letter_Number} or \P{Nl} etc. It is just limited to the general property classes.

Anyways, read more here facelessuser.github.io/RegReplac … references.

0 Likes

#72

2.2.1 update

  • Added some fixes for the extended backrefs where some situations it would not be able to detect inline (?x) or (?u) flags to alter the parsing logic. I doubt anyone ran into the issue as you would have to bury the flags in a non usual way, but anyways, it should be fine now.
0 Likes

#73

Hello,
I’m having problem with Reg Replace. I’ve received a lot of poems lyrics in .txt format (I’m not programist). Problem is they all have been saved with wrong encoding which resulted with replacing all national characters (I’m from Poland) to their equivalent in Unicode. I was looking for a tool which would allow me to batch all files at once and I bumped to Reg Replace for STE. I have installed Reg Replace using Package Controll and decided to do one file at the time. Fallowing this instructions:

see here

I’ve put in reg_replace.sublime-settings fallowing code:

`{
  "replacements": {
    "pl1": {
      "find": "ê",
      "replace": "ę",
      "greedy": true,
      "case": false,
      "literal": true
    },
    "pl2": {
      "find": "¿",
      "replace": "ż",
      "greedy": true,
      "case": false,
      "literal": true
    },
    "pl3": {
      "find": "³",
      "replace": "ł",
      "greedy": true,
      "case": false,
      "literal": true
    },
    "pl4": {
      "find": "¹",
      "replace": "ą",
      "greedy": true,
      "case": false,
      "literal": true
    },
    "pl5": {
      "find": "Ÿ",
      "replace": "ź",
      "greedy": true,
      "case": false,
      "literal": true
    },
    "pl6": {
      "find": "œ",
      "replace": "ś",
      "greedy": true,
      "case": false,
      "literal": true
    },
    "pl7": {
      "find": "æ",
      "replace": "ć",
      "greedy": true,
      "case": false,
      "literal": true
    }
  }
}`

Than I’ve put this code into Default.sublime-commands:

[
  {
    "caption": "Reg Replace: Polish characters",
    "command": "reg_replace",
    "args": {
      "replacements": [
        "pl1",
        "pl2",
        "pl3",
        "pl4",
        "pl5",
        "pl6",
        "pl7"
      ]
    },
    "find_only": true
  }
]

In the end I hit Ctrl + Shift + P and by typing “Polish characters” (name of my command), I find it, hit Enter and nothing happens. No error, no changes to opened document. Normally I would attempt some kind of trouble shooting, but I know only basis of Java Script and PHP. What I should check, where to read?

I’m using latest version of Sublime and running Windows 7 x64 Pro.

0 Likes

#74

@mateuszfryc, as tempting as it is to follow someone’s guide on the internet, there is no guarantee it is up to date – in this case, the guide you followed is very outdated.

  1. The official user guide is here: http://facelessuser.github.io/RegReplace/usage/#create-find-and-replace-sequences.

  2. Documentation states that replacement rules are now defined in reg_replace_rules.sublime-settings.

  3. You can’t use the argument case anymore as it is no longer valid. Case sensitivity is true by default as it is in the regular expression engine. If you need to do a non literal replacement sequence, you use Python’s regular expression case insensitive flag of (?i). If doing a literal example as you are doing, you use the flag literal_ignorecase option as the find rule will now be parsed literally not accounting the flag.

    Examp

    /* For literal: */
    "pl1": {
      "find": "ê",
      "replace": "ę",
      "greedy": true,
      "literal_ignorecase": true,
      "literal": true
    },
    /* non-literal */
    "pl1": {
      "find": "(?i)ê",
      "replace": "ę",
      "greedy": true
    },
    
  4. You made a mistake with your command, the find_only argument should have been under args.


In the next version to be released, I have a quick start guide available in Preferences -> Package Settings -> RegReplace -> Quick Start Guide but unfortunately it is taking a while for a new regex module dependency to get merged on Package Control. I can’t make a new release until that gets merged:

You can read the quick start guide here:


Anyways, this is what I did to get your example to work:

  1. Edit reg_relace_rules.sublime_settings and add this:

    {
        "format": "3.0",
        "replacements":
        {
            "pl1": {
              "find": "ê",
              "replace": "ę",
              "greedy": true,
              "literal_ignorecase": true,
              "literal": true
            },
            "pl2": {
              "find": "¿",
              "replace": "ż",
              "greedy": true,
              "literal_ignorecase": true,
              "literal": true
            },
            "pl3": {
              "find": "³",
              "replace": "ł",
              "greedy": true,
              "literal_ignorecase": true,
              "literal": true
            },
            "pl4": {
              "find": "¹",
              "replace": "ą",
              "greedy": true,
              "literal_ignorecase": true,
              "literal": true
            },
            "pl5": {
              "find": "Ÿ",
              "replace": "ź",
              "greedy": true,
              "literal_ignorecase": true,
              "literal": true
            },
            "pl6": {
              "find": "œ",
              "replace": "ś",
              "greedy": true,
              "literal_ignorecase": true,
              "literal": true
            },
            "pl7": {
              "find": "æ",
              "replace": "ć",
              "greedy": true,
              "literal_ignorecase": true,
              "literal": true
            }
        }
    }
    
  2. Place this command in Default.sublime-commands:

        {
            "caption": "Reg Replace: Polish characters",
            "command": "reg_replace",
            "args": {
                "replacements": [
                    "pl1",
                    "pl2",
                    "pl3",
                    "pl4",
                    "pl5",
                    "pl6",
                    "pl7"
                ],
                "find_only": true
            }
        }
    
  3. And with the following text in a file 꿳¹Ÿœæ, you can run the command and replace and get ężłąźść.

1 Like