Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incomplete Quantifier on Regex in vscode

I am trying to create a regex expression for the beginning of an object so I can replace the block complete with a single " in vscode's find and replace.

My Regex

("id":{"\$oid":)[0-9]+(},"product_id":)[0-9]+(,")

My sample input

{"id":{"$oid":973},"product_id":973,"product_name":"Scotch - Queen Anne","product_amount":92,"product_group":"not perishable","distribution_to":"The Salvation Army Visalia - Neighborhood Market","distribution_from":"MCFB","expiration_date":"8/24/2020","pack_date":"4/19/2021","sell_by_date":"12/6/2020","use_by_date":"2/18/2021","created_at":"2020-04-24 03:15:40 -0400","updated_at":"2020-04-24 03:15:40 -0400"},

I have tested this expression on these websites with success: regex101.com, regexr.com, however, I still get an error when I paste into my vscode.

enter image description here

like image 380
adarian Avatar asked Apr 25 '20 08:04

adarian


People also ask

Can you use regex in VSCode?

VSCode usageVscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.

What is quantifier in regex?

Quantifiers specify how many instances of a character, group, or character class must be present in the input for a match to be found.

What regex does VSCode use?

Visual Studio uses . NET regular expressions to find and replace text.

How do I change the regex code in Visual Studio?

How to enable VSCode regex replace. First, you need to press Ctrl + H on Windows and Linux, or ⌥⌘F on Mac to open up search and replace tool. In order to activate regex search and replace in VSCode, you have to click on the . * button near the input.


Video Answer


1 Answers

This is a valid regex, at least in the JavaScript engine used by VS Code (see this answer), but I think VS Code’s validation engine is confused (as was I) by the unescaped curly braces — the opening { being in the first group and the closing } in the second.

As suggested by the @rioV8 and @Nick in the comments you’ll need to escape them with the back slash \ to make it work:

("id":\{"\$oid":)[0-9]+(\},"product_id":)[0-9]+(,")

Here’s a little demo that demonstrates that the original regex works in JS:

const regex = /("id":{"\$oid":)[0-9]+(},"product_id":)[0-9]+(,")/;

const sample = '{"id":{"$oid":973},"product_id":973,"product_name":"Scotch - Queen Anne","product_amount":92,"product_group":"not perishable","distribution_to":"The Salvation Army Visalia - Neighborhood Market","distribution_from":"MCFB","expiration_date":"8/24/2020","pack_date":"4/19/2021","sell_by_date":"12/6/2020","use_by_date":"2/18/2021","created_at":"2020-04-24 03:15:40 -0400","updated_at":"2020-04-24 03:15:40 -0400"},';

const result = sample.match(regex);

let output = [
  `full match:\t${result[0]}`,
  `group 1:\t${result[1]}`,
  `group 2:\t${result[2]}`,
  `group 3:\t${result[3]}`
]

document.querySelector("#result").innerText = output.join('\n');
<pre id=result></pre>
like image 142
sainaen Avatar answered Oct 22 '22 17:10

sainaen