Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using (?s) Regex options in Visual Studio Code

I am used to writing regular expressions that support multiple options to specify case sensitivity, white space ignoring, meaning of . etc... In C#, these options are specified with (?i), (?x) and (?s) respectively.

How can these modifiers be used with Visual Studio Code find functionality? I am getting an error

Invalid regular expression: Invalid group.

Example:

q.*?abc.*?q 

will match <q>heheabchihi</q>, but not

<q>hehe
  abchihi</q>

due to the . not matching all characters (\n is omitted). Adding (?s) fixes that in C# regex, but not in Visual Studio Code. What is the Visual Studio Code way of using regex options?

like image 622
Robert Segdewick Avatar asked Aug 10 '26 18:08

Robert Segdewick


2 Answers

Updated answer

in my original answer, I have overseen the visual studio code requirement, and here I update my answer based on that.

I have made this regex to find the match in Visual Studio Code.

q.*?(.|\n)+?.q

This will find the following:

enter image description here

Tested also here https://regex101.com/r/f3vKcU/1

Inspired by this answer.

Hope that helps.


Original answer

You can do something simple as adding [^<>] in your regex.

So change this

q.*?abc.*?q

to

q.[^<>]*?abc.*?q

Will do the job.

Check it https://regex101.com/r/DWQ9ZP/1

I got the inspiration from this answer.

like image 94
Maytham Avatar answered Aug 12 '26 08:08

Maytham


you could match any character or newline like this:

<q>[\s\S\n]*<\/q>

this would select everything inside the q tag until it is closed

i used [\s\S] to select every other character aside \n, it would translate to every whitespace-character and every non-whitespace-character


added note: since * is a quantifier for 0 or more, the ? becomes unnecessary

like image 20
aarondiel Avatar answered Aug 12 '26 07:08

aarondiel