Hi all
I've written a short ruby script for the purposes of checking regular expressions within TextMate.
http://dml.homedns.org/rx_helper.rb
It expects to be fed a regular expression on a line of its own, followed by any number of lines to test against. The RX can be either a straight match (eg ^([a-z]+):([0-9]+ ) or a substitution (eg, /old/new/). Cuddling /slashes/ are only required for /pattern/replacement/ expressions.
If you create the following action in TextMate:
echo "$TM_SELECTED_TEXT" | /path_to_script/rx_helper.rb
with input='selected text' & output='show as tooltip', you can open a new window, write a regexp, write some test cases for your regexp, then select-all and see the results in a tooltip whenever you need it (I have it bound to ctrl-alt-option-X).
I hope someone finds it useful. If you find any bugs / have any suggestions on how to improve it, please do let me know.
cheers
D
P.S here's some sample input and output: ------------------------------------ IN (TextMate scratch file) == (http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3}) http://www.google.com https://www.netbank.com.au ftp://mafia.ru www.clickstart.com boozehound gary sweeney http://www.amazon.com/ http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779 -6984007 ~
OUT (tooltip) == (http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3}) Successful Matches: =================== http://www.google.com : matched expression match number [1] : http match number [2] : google. match number [3] : google match number [4] : com https://www.netbank.com.au : matched expression match number [1] : https match number [2] : com. match number [3] : com match number [4] : au ftp://mafia.ru : matched expression match number [1] : ftp match number [2] : mafia. match number [3] : mafia match number [4] : ru http://www.amazon.com/ : matched expression match number [1] : http match number [2] : amazon. match number [3] : amazon match number [4] : com http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779 -6984007 : matched expression match number [1] : http match number [2] : amazon. match number [3] : amazon match number [4] : com
Failed Matches: =============== www.clickstart.com : failed boozehound : failed gary sweeney : failed
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
David, Looks great....Now the stupid question. How do I use it?
Should I place this in TextMate as a snippet and then type in a line of regexp to test and run the rx_helper over it, or install this a as a command within the ruby bundle. Alternately, should I have my file open in textmate and then write my rexexp in the "filter through command" after calling rx_helper.
Thanks for the work, and the time to explain its use
Robert
On Apr 3, 2005, at 6:37 AM, David Lee wrote:
Hi all
I've written a short ruby script for the purposes of checking regular expressions within TextMate.
http://dml.homedns.org/rx_helper.rb
It expects to be fed a regular expression on a line of its own, followed by any number of lines to test against. The RX can be either a straight match (eg ^([a-z]+):([0-9]+ ) or a substitution (eg, /old/new/). Cuddling /slashes/ are only required for /pattern/replacement/ expressions.
If you create the following action in TextMate:
echo "$TM_SELECTED_TEXT" | /path_to_script/rx_helper.rb
with input='selected text' & output='show as tooltip', you can open a new window, write a regexp, write some test cases for your regexp, then select-all and see the results in a tooltip whenever you need it (I have it bound to ctrl-alt-option-X).
I hope someone finds it useful. If you find any bugs / have any suggestions on how to improve it, please do let me know.
cheers
D
P.S here's some sample input and output:
IN (TextMate scratch file)
(http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3}) http://www.google.com https://www.netbank.com.au ftp://mafia.ru www.clickstart.com boozehound gary sweeney http://www.amazon.com/ http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779 -6984007 ~
OUT (tooltip)
(http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3}) Successful Matches: =================== http://www.google.com : matched expression match number [1] : http match number [2] : google. match number [3] : google match number [4] : com https://www.netbank.com.au : matched expression match number [1] : https match number [2] : com. match number [3] : com match number [4] : au ftp://mafia.ru : matched expression match number [1] : ftp match number [2] : mafia. match number [3] : mafia match number [4] : ru http://www.amazon.com/ : matched expression match number [1] : http match number [2] : amazon. match number [3] : amazon match number [4] : com http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779 -6984007 : matched expression match number [1] : http match number [2] : amazon. match number [3] : amazon match number [4] : com
Failed Matches:
www.clickstart.com : failed boozehound : failed gary sweeney : failed
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
Robert,
I updated the docblock in the script with installation instructions (including Allan's handy hint about STDIN).
It's basically a standalone script which TM pipes stuff into / out of with a tiny Command.
So you open a new TM document, write your regexp on the first line, write some match/replace-fodder below it, select-all and run the Command.
please let me know if you get stuck
cheers
D
On 04/04/2005, at 1:57 AM, Robert Ullrey wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
David, Looks great....Now the stupid question. How do I use it?
Should I place this in TextMate as a snippet and then type in a line of regexp to test and run the rx_helper over it, or install this a as a command within the ruby bundle. Alternately, should I have my file open in textmate and then write my rexexp in the "filter through command" after calling rx_helper.
Thanks for the work, and the time to explain its use
Robert
On Apr 3, 2005, at 6:37 AM, David Lee wrote:
Hi all
I've written a short ruby script for the purposes of checking regular expressions within TextMate.
http://dml.homedns.org/rx_helper.rb
It expects to be fed a regular expression on a line of its own, followed by any number of lines to test against. The RX can be either a straight match (eg ^([a-z]+):([0-9]+ ) or a substitution (eg, /old/new/). Cuddling /slashes/ are only required for /pattern/replacement/ expressions.
If you create the following action in TextMate:
echo "$TM_SELECTED_TEXT" | /path_to_script/rx_helper.rb
with input='selected text' & output='show as tooltip', you can open a new window, write a regexp, write some test cases for your regexp, then select-all and see the results in a tooltip whenever you need it (I have it bound to ctrl-alt-option-X).
I hope someone finds it useful. If you find any bugs / have any suggestions on how to improve it, please do let me know.
cheers
D
P.S here's some sample input and output:
IN (TextMate scratch file)
(http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3}) http://www.google.com https://www.netbank.com.au ftp://mafia.ru www.clickstart.com boozehound gary sweeney http://www.amazon.com/ http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779 -6984007 ~
OUT (tooltip)
(http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3}) Successful Matches: =================== http://www.google.com : matched expression match number [1] : http match number [2] : google. match number [3] : google match number [4] : com https://www.netbank.com.au : matched expression match number [1] : https match number [2] : com. match number [3] : com match number [4] : au ftp://mafia.ru : matched expression match number [1] : ftp match number [2] : mafia. match number [3] : mafia match number [4] : ru http://www.amazon.com/ : matched expression match number [1] : http match number [2] : amazon. match number [3] : amazon match number [4] : com http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779 -6984007 : matched expression match number [1] : http match number [2] : amazon. match number [3] : amazon match number [4] : com
Failed Matches:
www.clickstart.com : failed boozehound : failed gary sweeney : failed
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
-----BEGIN PGP SIGNATURE----- Version: PGP 8.1
iQA/AwUBQlASWmO1AQX/YDblEQK6HwCeOI17A5+LGZfOy8MaQXQMKPGaLCIAn0mn We4Jdx/9UCM9/r7cxGghP7wH =L5Pz -----END PGP SIGNATURE-----
For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
On Apr 3, 2005, at 15:37, David Lee wrote:
[...] echo "$TM_SELECTED_TEXT" | /path_to_script/rx_helper.rb
with input='selected text' & output='show as tooltip'
The »echo "$TM_SELECTED_TEXT" |« part should be redundant, since stdin is already the selected text (which is a preferred way to work on the selection, since variables have an upper limit of 256K (okay, not likely you'll experience it, but still...) and echo $something will mangle the text slightly, like remove newlines.
Hi David,
I tried your script and got an error:
/Users/erichsu/Library/Application Support/TextMate/Bundles/Regular Expressions.tmbundle/Tools/rx_helper.rb:62:in `prepare_rx': undefined method `match' for #String:0xc400c (NameError) from /Users/erichsu/Library/Application Support/TextMate/Bundles/Regular Expressions.tmbundle/Tools/rx_helper.rb:45:in `initialize' from /Users/erichsu/Library/Application Support/TextMate/Bundles/Regular Expressions.tmbundle/Tools/rx_helper.rb:114:in `new' from /Users/erichsu/Library/Application Support/TextMate/Bundles/Regular Expressions.tmbundle/Tools/rx_helper.rb:114
when running on your sample text.
Any thoughts? I want to put this into the svn repository when it's working. If it matters, I'm running:
ruby 1.6.8 (2002-12-24) [powerpc-darwin7.0] which lives at /usr/bin/ruby
best, Eric
On 4 avr. 05, at 19:57, Eric Hsu wrote:
Hi David,
I tried your script and got an error:
(...)
when running on your sample text.
(...)
If it matters, I'm running:
ruby 1.6.8 (2002-12-24) [powerpc-darwin7.0] which lives at /usr/bin/ruby
No problem here using ruby 1.8.2 (2004-12-25) [powerpc-darwin7.7.0] in /usr/local/bin/ruby
Fred.
At 12:51 AM +0200 4/5/05, Fred B. wrote:
No problem here using ruby 1.8.2 (2004-12-25) [powerpc-darwin7.7.0] in /usr/local/bin/ruby
It must be the version then... Maybe Tiger will update Ruby for me. :)
- Eric
Hi Eric - are you using the stock OS X 10.3.x install of ruby ? I'm running the latest stable release.
I'll test against the stock install when I get home. Shouldn't be too hard to get it playing nice with both; then I can submit it to svn :)
Cheers
D
-----Original Message----- From: Eric Hsu [mailto:erichsu@math.sfsu.edu] Sent: Tuesday, 5 April 2005 8:55 AM To: TM Users Subject: Re: [TxMt] a regexp test script (link)
At 12:51 AM +0200 4/5/05, Fred B. wrote:
No problem here using ruby 1.8.2 (2004-12-25) [powerpc-darwin7.7.0] in /usr/local/bin/ruby
It must be the version then... Maybe Tiger will update Ruby for me. :)
- Eric -- Eric Hsu, Assistant Professor of Mathematics San Francisco State University erichsu@math.sfsu.edu http://math.sfsu.edu/hsu ______________________________________________________________________ For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
At 2:33 PM +1000 4/5/05, David Lee wrote:
I'll test against the stock install when I get home. Shouldn't be too hard to get it playing nice with both; then I can submit it to svn :)
Great, just commit over the copy I put in the RegularExpressions.tmbundle...
- Eric
I've written an alternative version of the regular expression checker. It relies heavily on a script I found on the web by Aristotle at perlmonks.org (cited in the code).
I've checked it into the svn repository. There's also a version of the Ruby script that produces a tooltip (though I can't get that to work on my computer yet). They live at http://macromates.com/svn/Bundles/trunk/RegularExpressions.tmbundle/
The way this alternative works is you copy the regexp to the clipboard, then highlight the text you want to check. Then it produces HTML output which color codes each subcapture.
There might be a nicer way to grab the regexp, like maybe using Cocoadialog, but I just wanted to get this out there.
best wishes, Eric
ps. Here's what you get with the sample file David Lee posted:
CLIPBOARD (http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3})
INPUT http://www.google.com https://www.netbank.com.au ftp://mafia.ru www.clickstart.com boozehound gary sweeney http://www.amazon.com/ http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779-6984007
OUTPUT. <html><head></head><body> <h3>Results for /(http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3})/</h3><tt> <b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">google.</font></b><b><font color="Red"></font></b><b><font color="Brown">google</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b> <br/><b><font color="Red"></font></b><b><font color="Dark Blue">https</font></b><b><font color="Red">://www.netbank.</font></b><b><font color="Dark Purple">com.</font></b><b><font color="Red"></font></b><b><font color="Brown">com</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">au</font></b><b><font color="Red"></font></b> <br/><b><font color="Red"></font></b><b><font color="Dark Blue">ftp</font></b><b><font color="Red">://</font></b><b><font color="Dark Purple">mafia.</font></b><b><font color="Red"></font></b><b><font color="Brown">mafia</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">ru</font></b><b><font color="Red"></font></b> <br/>www.clickstart.com <br/>boozehound <br/>gary sweeney <br/><b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">amazon.</font></b><b><font color="Red"></font></b><b><font color="Brown">amazon</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b>/ <br/><b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">amazon.</font></b><b><font color="Red">com/exec/obidos/subst/home/home.html/002-0270779-6</font></b><b><font color="Brown">amazon</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b>/exec/obidos/subst/home/home.html/002-0270779-6984007 <br/></tt></body></html>
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Very nice. Thank you for the bundle. Robert
On Apr 4, 2005, at 2:31 PM, Eric Hsu wrote:
I've written an alternative version of the regular expression checker. It relies heavily on a script I found on the web by Aristotle at perlmonks.org (cited in the code).
I've checked it into the svn repository. There's also a version of the Ruby script that produces a tooltip (though I can't get that to work on my computer yet). They live at http://macromates.com/svn/Bundles/trunk/RegularExpressions.tmbundle/
The way this alternative works is you copy the regexp to the clipboard, then highlight the text you want to check. Then it produces HTML output which color codes each subcapture.
There might be a nicer way to grab the regexp, like maybe using Cocoadialog, but I just wanted to get this out there.
best wishes, Eric
ps. Here's what you get with the sample file David Lee posted:
CLIPBOARD (http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3})
INPUT http://www.google.com https://www.netbank.com.au ftp://mafia.ru www.clickstart.com boozehound gary sweeney http://www.amazon.com/ http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779 -6984007
OUTPUT.
<html><head></head><body> <h3>Results for /(http|https|ftp):\/\/(([-_a-zA-Z1-9]+)\.)+([-_a-zA-Z1-9]{2,3})/</ h3><tt> <b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">google.</font></b><b><font color="Red"></font></b><b><font color="Brown">google</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b> <br/><b><font color="Red"></font></b><b><font color="Dark Blue">https</font></b><b><font color="Red">://www.netbank.</font></b><b><font color="Dark Purple">com.</font></b><b><font color="Red"></font></b><b><font color="Brown">com</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">au</font></b><b><font color="Red"></font></b> <br/><b><font color="Red"></font></b><b><font color="Dark Blue">ftp</font></b><b><font color="Red">://</font></b><b><font color="Dark Purple">mafia.</font></b><b><font color="Red"></font></b><b><font color="Brown">mafia</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">ru</font></b><b><font color="Red"></font></b> <br/>www.clickstart.com <br/>boozehound <br/>gary sweeney <br/><b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">amazon.</font></b><b><font color="Red"></font></b><b><font color="Brown">amazon</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b>/ <br/><b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">amazon.</font></b><b><font color="Red">com/exec/obidos/subst/home/home.html/002-0270779-6</ font></b><b><font color="Brown">amazon</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b>/exec/obidos/subst/home/home.html/002-0270779 -6984007 <br/></tt></body></html>
-- Eric Hsu, Assistant Professor of Mathematics San Francisco State University erichsu@math.sfsu.edu http://math.sfsu.edu/hsu ______________________________________________________________________ For new threads USE THIS: textmate@lists.macromates.com (threading gets destroyed and the universe will collapse if you don't) http://lists.macromates.com/mailman/listinfo/textmate
Nice one Eric.
I'll have a look at my script when I get home & work out if it's worth cleaning up & submitting to svn. I didn't get any such errors but I'm stuck on windows here :( so textmate will have to wait till later.
Cheers
D
-----Original Message----- From: Eric Hsu [mailto:erichsu@math.sfsu.edu] Sent: Tuesday, 5 April 2005 7:32 AM To: TM Users Subject: Re: [TxMt] a regexp test script (link)
I've written an alternative version of the regular expression checker. It relies heavily on a script I found on the web by Aristotle at perlmonks.org (cited in the code).
I've checked it into the svn repository. There's also a version of the Ruby script that produces a tooltip (though I can't get that to work on my computer yet). They live at http://macromates.com/svn/Bundles/trunk/RegularExpressions.tmbundle/
The way this alternative works is you copy the regexp to the clipboard, then highlight the text you want to check. Then it produces HTML output which color codes each subcapture.
There might be a nicer way to grab the regexp, like maybe using Cocoadialog, but I just wanted to get this out there.
best wishes, Eric
ps. Here's what you get with the sample file David Lee posted:
CLIPBOARD (http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3})
INPUT http://www.google.com https://www.netbank.com.au ftp://mafia.ru www.clickstart.com boozehound gary sweeney http://www.amazon.com/ http://www.amazon.com/exec/obidos/subst/home/home.html/002-0270779-6984007
OUTPUT. <html><head></head><body> <h3>Results for /(http|https|ftp)://(([-_a-zA-Z1-9]+).)+([-_a-zA-Z1-9]{2,3})/</h3><tt> <b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">google.</font></b><b><font color="Red"></font></b><b><font color="Brown">google</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b> <br/><b><font color="Red"></font></b><b><font color="Dark Blue">https</font></b><b><font color="Red">://www.netbank.</font></b><b><font color="Dark Purple">com.</font></b><b><font color="Red"></font></b><b><font color="Brown">com</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">au</font></b><b><font color="Red"></font></b> <br/><b><font color="Red"></font></b><b><font color="Dark Blue">ftp</font></b><b><font color="Red">://</font></b><b><font color="Dark Purple">mafia.</font></b><b><font color="Red"></font></b><b><font color="Brown">mafia</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">ru</font></b><b><font color="Red"></font></b> <br/>www.clickstart.com <br/>boozehound <br/>gary sweeney <br/><b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">amazon.</font></b><b><font color="Red"></font></b><b><font color="Brown">amazon</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b>/ <br/><b><font color="Red"></font></b><b><font color="Dark Blue">http</font></b><b><font color="Red">://www.</font></b><b><font color="Dark Purple">amazon.</font></b><b><font color="Red">com/exec/obidos/subst/home/home.html/002-0270779-6</font></b><b> <font color="Brown">amazon</font></b><b><font color="Red">.</font></b><b><font color="Burgundy">com</font></b><b><font color="Red"></font></b>/exec/obidos/subst/home/home.html/002-0270779-6984007 <br/></tt></body></html>