Sublime Forum

Mulit-line overlapping regex problem

#1

I don’t know if this is a general regex issue or how Sublime Text uses the syntax. Basically what I’m trying to do is capture:

{$ifdef foo} … {$end}

where … is within the begin/end patterns. This is not a problem and works as expected UNLESS the begin/end syntax is on the same line and then the “begin” pattern captures into the “end” pattern OR (not sure which) the “end” pattern starts from the “begin” pattern and causes overlapping.

Can anyone suggested a solution? I’ve tried everything I can think of and still not getting it to work.

	<dict>
		<key>begin</key>
		<string>\{\$\b(?i:(if|ifdef|ifndef))\b\s*([^}]+)\}</string>
		<key>captures</key>
		<dict>
			<key>0</key>
			<dict>
				<key>name</key>
				<string>comment.macro.pascal</string>
			</dict>
			<key>2</key>
			<dict>
				<key>name</key>
				<string>comment.macro.value.pascal</string>
			</dict>
		</dict>
		<key>end</key>
		<string>\{\$\b(?i:(endif))\b.*\}</string>
		<key>endCaptures</key>
		<dict>
			<key>0</key>
			<dict>
				<key>name</key>
				<string>comment.macro.pascal</string>
			</dict>
		</dict>
		<key>name</key>
		<string>meta.scope.conditional.pascal</string>
		<key>patterns</key>
		<array>
			<dict>
				<key>include</key>
				<string>$self</string>
			</dict>
		</array>
	</dict>
0 Likes

#2

Sorry I got this resolved. There was another conflicting pattern that was giving false positives.

0 Likes

#3

As a hint, if you’re not bound to the old tmLanguage format due to supporting other editors besides ST, I highly suggest using the new sublime-syntax format instead. There is a command to convert an old syntax definition to a new one under “Tools > Developer” that is active when you have a view active that uses said syntax. Documentation: https://www.sublimetext.com/docs/3/syntax.html

I also suggest installing https://github.com/SublimeText/PackageDev/. If you’re bound to tmLanguage, you can use it to at least work in a yaml format and convert that to the required plist on demand.

1 Like

#4

Thanks for the tip, I’ll looking into this.

0 Likes