web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Multi-line text search in VSCode (with RegEx)

waldo Profile Picture waldo 6,430

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>

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 ;-).


This was originally posted here.

Comments

*This post is locked for comments