We could write a custom plugin using the Sublime API or try to use a plugin that promises to offer this functionality already: https://github.com/facelessuser/RegReplace
Demonstration
We have a following structured but not consistently formatted data that we would like to adjust so it is easier toread and work with.
To reformat the text we can use the above plugin and define a series of regex that match and modify text.
- Installed RegReplace plugin.
- Create a reg_replace.sublime-settings in your Sublime2\Data\Packages\User\ directory and define the regex commands we want to use.
{ "replacements": { // add teh .<digit> when is missing "ig_order_add_dot_digit": { "find": "([0-9][0-9]) at", "replace": "\\1.0 at" // "greedy": true, // "case": false }, "ig_order_add_dot_digit2": { "find": "([0-9][0-9]) *- ", "replace": "\\1.0 - ", "greedy": true }, "ig_order_fix_spaces": { "find": "/(201[0-9]) *", "replace": "/\\1 " }, "ig_order_fix_spaces2": { "find": "- - - ", "replace": "- - - " }, "ig_order_change_android_str": { "find": "AndroidApp", "replace": "AndrAp" }, "ig_order_remove_str": { "find": "/s ", "replace": " ", "greedy": true }, "ig_order_fix_header": { "find": "(Date) *(Time) *(Activity) *(Market) *(Period) *(Channel) *(Currency) *(Size) *(Level) *(Stop) *(Type) *(Limit) *(Result)", "replace": "Date Time Activity Market Period Channel Cur Size Level Stop Type Limit Result", "greedy": true }, "ig_transactions_fix_header": { "find": "(Type) *(Date) *(Ref) *(Market) *(Period) *(Opening) *(Ccy) *(Size) *(Closing) *(P/L)", "replace": "Type Date Ref Market Period Opening Ccy Size Closing P/L", "greedy": true }, "ig_transactions_add_dot_digit": { "find": "([0-9][0-9]) +£", "replace": "\\1.0 £" }, "ig_transactions_add_dot_digit2": { "find": "(£ +.*\\..* +)([0-9]+) +", "replace": "\\1\\2.0 " }, "ig_transactions_fix_plus_minus_sign": { "find": "([0-9]+\\.[0-9]+ +[0-9]+\\.[0-9]+ +)([0-9]+\\.[0-9]+)", "replace": "\\1 \\2" } } }
- Define the final regex command to run and associate a a keyboard short in Default (Windows).sublime-keymap file
[ { { "keys": ["alt+ctrl+t"], "command": "reg_replace", "args": {"replacements": [ // orders "ig_order_add_dot_digit", "ig_order_add_dot_digit2", "ig_order_fix_spaces", "ig_order_fix_spaces2", "ig_order_change_android_str", "ig_order_remove_str", "ig_order_fix_header", // transactions "ig_transactions_fix_header", "ig_transactions_add_dot_digit", "ig_transactions_add_dot_digit2", "ig_transactions_fix_plus_minus_sign" ], "find_only": true} } ]
- When you activate the regex chain command it will first show what part of the file are going to be changed
- Accept the "yes" option at the bottom and reformat the file
No comments:
Post a Comment