views:

115

answers:

7

Regular expressions are great for text editing. But sometimes they aren't quite enough. For example, say I have a text with 1000 sentences, with the first character in lowercase:

my name is Foo. hello, how are you? i really like this forum. ... etc.

And I want to convert them so that the first letter is capitalized. This IS possible with regular expressions, but the regular expression you need is very long and untidy. One of the things I like the most of regexs is the possibility to catch parts of the string in parenthesis and use them with $i or \i or whatever for replacement.

My question is: do you know an editor or plugin for an editor where you can process this capturing groups in some sort of language (say, Python?). This would allow us to do such things as:

search for:
^(.)(.*)$
and replace that with:
toUpperCase($1)$2

If you know one such system, please let me know. Thanks in advance,

Manuel

+3  A: 

Try GVim - if you are (or are prepared to get) comfortable with the vi shortcuts.

http://www.vim.org/

Another one of course is perl from the command line.

Can you actually run code on the captured groups?
Robert Harvey
Oh sorry - no code on groups in np++ and vs - as far as I know.
+4  A: 

Awk offers such a mix of Regex integration with imperative programming.

mjv
Awk is certainly a pending assignment for me. Thanks!
Manuel
+5  A: 

Regexbuddy for the win. Jeff Atwood's blog (Coding Horror) mentions it a few times.

brad.huffman
A great example of software that does one thing very well. Love this app.
Nicolas Webb
Can you actually run code on the captured groups?
Robert Harvey
A: 

Various editors have their own extensions to regex syntax, but I find it easier to write a short program more often than 1) learning a new syntax, or 2) learning a new editor. But then I already know how to program. :)

Textpad has escape codes like \U for uppercase, for your example, along with others.

Roger Pate
+1  A: 

UltraEdit has a complete core JavaScript engine built-in, including regex capabilities. The scripting engine has full access to UE's DOM, so it looks like that might work for you.

Tim Pietzcker
+1  A: 

Komodo Edit allows you to create code snippets in several scripting languages (including Python, Perl, and PHP) that you can execute on selected text (or, I believe, across files.)

So if you want to run a small script in your editor that went through every line (or every 27 lines) and capitalized the first letter you could do it in this editor and save it as a code snippet to run later.

Further, it allows you to create dialogues to fill out portions of your code so you could write the code once and then run arbitrary python code on whatever regular expression you needed without ever touching the base script again.

Sean Vieira
+2  A: 

GNU Emacs can do it as well (if you don't mind Emacs Lisp and the uncommon GUI). It still uses the old POSIX Regex syntax, not the new Perl one (which means you have to escape capturing group parentheses).

Search

^\(.\)

Replace by

\,(upcase \1)

and you are done. You can of course do it interactively, and you can also call your own Lisp macros from there, of course.

mihi
You don't need Emacs Lisp for this. Type C-x ( to start recording a macro, do the operation manually once (M-c to capitalize word, C-s . to go to the next sentence), C-x ) to stop recording, then C-u 1000 C-x e to run it 1000 times.
Ken
True for this special case. But you can use Lisp (which can do arbitarily complex things) inside of regex replacements. And, you can use F3 and F4 in recent emacsen to record and play macros - unless you remapped that keys of course.
mihi