[TxMt] regex question

Dr. Drang drdrang at gmail.com
Tue Feb 21 20:26:56 UTC 2006


On Feb 21, 2006, at 12:50 PM, Oliver Taylor wrote:

>>> ------------------------
>>> ONCE
>>> (twice)
>>> Three times a lady.
>>> Not as much as the Gambler.
>>> (This sucks)
>>> How would you know?
>>> ------------------------
>
> Matching the 2nd line (but not the 5th): I want to match any line  
> that begins and ends with a parenthetical ... ^\(.*\)$ ... but only  
> when the line is directly preceded by a line of all capitol letters,
>
> Matching the 6th (but not the 3rd): Actually, I got this one  
> backwards. I want to match the 3rd but not the 6th. So... Any line  
> that is directly preceded by the match I described above.
>

The matching string

     ^([[:upper:]]+)\n(\(.+\))\n(.+)$

will put the capitalized, parenthesized, and trailing lines in $1,  
$2, and $3, respectively. (You'll have to adjust it if the  
capitalized line can have spaces.) Depending on what you want to do  
with the lines after they've been captured, this may be good enough.

If you need to match, for example, the parenthesized line *without*  
including in $0 the capitalized line before or the line after, you  
may be able to use a look-behind, which is covered in Section 7 of  
the Oniguruma manual referenced by Paul Knight. However, the manual  
says "Subexp of look-behind must be fixed character length," so you  
may be out of luck with that approach. You might need to use Perl,  
which--as far as I know--doesn't have that restriction on look-behinds.

--
Dr. Drang





More information about the textmate mailing list