ONCE (twice) Three times a lady. Not as much as the Gambler. (This sucks) How would you know?
How do I match the 2nd line but not the 5th? How do I match the 6th, but not he 3rd?
What is different in each case? The presence of ONCE exactly in the line before?
Indeed, I'm uncertain what the logic behind the matches is supposed to be. Is the second line matched because of the sequence of cardinal numbers, a parenthetical following a line of all capital letters, a parenthetical with only one word, a parenthetical with no capitalized letters, a parenthetical followed by a statement... and so on.
Yeah, sorry.
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.
----
I'll check out the IRC channel.
I'll check it out.
On 21 Feb 2006, at 18:50, Oliver Taylor wrote:
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,
This
^[A-Z]+\n([^)]*)$
does the trick. Broken down
^ matches start of the first line [A-Z]+ matches a run of one or more upper case letters \n matches the end of the first line ( matches the opening parenthesis [^)]* matches a run of zero or more characters matching anything apart from a closing parenthesis ) matches the closing parenthesis $ matches the end of the second line
If you want to capture the upper case letters (so you can refer to them as $1 in the replacement pattern) add brackets like this:
^([A-Z]+)\n([^)]*)$
If you want to capture the contents of the brackets add brackets like this:
^[A-Z]+\n(([^)]*))$
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