Sublime Forum

Selecting Text Between 2 Strings Using Find & Regex

#1

This maybe technically a regex question, but I am using it in ST3 and I’m really really stuck (>2hrs).

Shot from ST3 (the format is .vcf)

Better Format

BEGIN:VCARD
VERSION:2.1
N:;Coop Bank 24hr;;;
FN:Coop Bank 24hr
TEL;CELL;PREF:03457212212
TEL;CELL:0169553760
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:;Coop Bank Frank;;;
FN:Coop Bank Frank
TEL;CELL;PREF:01179293519
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:Tax;Council;;;
FN:Council Tax
TEL;CELL:01225477777
END:VCARD
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;=43=C3=B6=6E=64=6F=72=20=46=65=72=72=79;;;
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=43=C3=B6=6E=64=6F=72=20=46=65=72=72=79
TEL;CELL;PREF:01202207299
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:;D V L A;;;
FN:D V L A
TEL;CELL;PREF:03001230883
END:VCARD

I’m trying to select text between BEGIN:VCARD and END:VCARD (for example)

I’ve tried (using the find (regex) function in ST3)

(?<=BEGIN)\n*.*(?=END)
(?<=BEGIN)\n{2,}(?=END)
(?<=BEGIN).*(?=END)
(?<=BEGIN)\s\S.*(?=END)

(?<=sms protocol)(.*)(?=New) This selected text between sms protocol and New when it was all on one line (in a completely different file)(thus I can get it to work when there are no newline characters involved).
But I can’t get the regex to work for capital letters and any number of newlines.
I’ve also looked at a lot of StackOverflow questions, but to no avail.

Hopefully this question will be handy for other fellow noobs too as the power (or one of it’s powers) of ST relies on regex…

ATB Loz

Ps All the phone numbers are in the public domain (or obsolete), just in case any folk were worrying

0 Likes

#2

. won’t match new lines. Use .|\n to match any char. For example, (?<=BEGIN:VCARD)(.|\n)*?(?=END:VCARD)

1 Like

#3

Thanks for the quick reply. I’d tried (?<=VERSION)+(.|\n)*(?=END:VCARD) !
(I know coz I up arrowed when in 'find mode’ to cycle through previous find options).

For completion I’m using
(?<=END:VCARD)(.|\n)*?(?=END:VCARD)+END:VCARD
which selects from (and including) BEGIN:VCARD to END:VCARD, ie the entirity of a contact.
Thanks @jfcherng, much appreciated !

0 Likes

#4

I may have done a circuit of the intelligent/stupid loop, and got my way back to stupid:

(BEGIN:VCARD)(.|\n)*?(END:VCARD)

works very well too. I’m not sure why I was using the lookaround regex functionality…

0 Likes