Multi-line text search in VSCode (with RegEx)

Small post – just because I needed it recently – and it made me think of this little gem that I still had to share: what if you have to search over multiple lines in multiple files in VSCode .. something that actually might happen more than you want to admit.

I actually never knew how to do this decently, until I came across this tweet:

The core of the “solution” is this RegEx: [\s\S\n]+?

To explain you simply:

  • \s: matches any whitespace character (space, table, line breaks)
  • \S: matches any character that is not a whitespace character
  • \n: matches a line feed character (code 10)
  • []: matches any character in this set
  • +: matches one or more of the preceding token – in this case, the set of any character including the line feed
  • ?: causing the preceding quantifier to match as few as possible. So – take all, but as few as you can.

Here are a few examples:

Find all translation-info spread over multiple lines..

<trans-unit id="Enum[\s\S\n]+?<\/source>

<trans-unit id= "Enum 
1931 results in 75 files - Open in editor 
BASE - App 
iFacto Base.fr-BE.xlf BASE - App 
<trans-unit 50607090 
<trans-unit id="Enum 50607090 
<trans-unit 50607090 
• Translations 141 
- Enumvalue 3263198591 
- Enumvalue 4190668035 
- Enumvalue 2004364593 
- Property 28 
- Propefty 28 
- Property 28

Or find “CLEAN19” code snippets with

if not CLEAN19[\s\S\n]+?#endif

And don’t forget to put your search in VSCode in “RegEx” mode (ALT+R)!

Thank you, phenno, for sharing! It might not work for all cases, it might need some finetuning – but for the searches I needed, it always did its job ;-).

4.75 avg. rating (94% score) - 12 votes

Permanent link to this article: https://www.waldo.be/2022/01/31/multi-line-text-search-in-vscode-with-regex/

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.