18

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

Nick
  • 138,499
  • 22
  • 57
  • 95
adarian
  • 334
  • 7
  • 24
  • 10
    `{` should be escaped. – Nick Apr 25 '20 at 08:51
  • 2
    Nick is right, this means regex101 is incorrect, `{` and `}` have meaning so need to be escaped. regex101 only sees this as a quantifier if it contains a number or 2 numbers separated by a `,` – rioV8 Apr 25 '20 at 09:02
  • @rioV8: It depends on regex flavour. Most of them allow curly braces to not be escaped. For me it's an error from VSC. – Toto Apr 25 '20 at 09:19
  • @Toto could you please give an example of a flavor that does allow anything else besides quanitifers inside `{}`? UPD: ah, sorry, I’ve missed that the curly braces are in _different_ groups; it looks like I’ve made the same mistake as VS Code :) – sainaen Apr 25 '20 at 11:28
  • You have a fairly complex regex. Is your sample input mixed with other unwanted input? `^\{([^,]*,[^,]*,")` works on your sample input but perhaps it would match other text in your file if there is such. – Mark Apr 25 '20 at 16:14

1 Answers1

23

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>
sainaen
  • 1,498
  • 9
  • 18