-2

i need a regex that replaces everything except the content between the first " and the last ".

I need it like this:

Input String:["Key:"Value""]

And after the regex i only need this:

Output String:Key:"Value"

Thanks!

  • 1
    in your question you say "bracket", in the title you say "double quotes", and in the example your replacing both the brackets and the double quotes. Which is it? – doom87er Oct 23 '18 at 21:41
  • @doom87er i mean double quote sorry for the confusing – Danique de Jong Dan Oct 23 '18 at 22:11
  • Possible duplicate of [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – Biffen Oct 24 '18 at 06:43

2 Answers2

1

You can try something like this.

patern:
^.*?"(.*)".*$

Substion:
$1

On Regex101

Explination:

the first part ^.*?" matches as few characters as possible that are between the start of the string and a double quote

the second part(.*)" makes the largest match it can that ends in a double quote, and stuffs it all in a capture group

the last part .*$ grabs what ever is left and includes it in the match

Finally you replace the entire match with the contents of the first capture group

doom87er
  • 458
  • 2
  • 8
  • Very good regexp! The `.` does not match newlines, but the original question didn't specify whether that's relevant. If it is, you can use `[^]` instead to match any character. Dart's replace does not have `"$1"` as a replacement pattern. You can do `input.replaceFirstMapped(re, (m) => m[1])` to replace with the first group. – lrn Oct 24 '18 at 06:25
  • i'm un-familiar with dart. but, if it has Regex options like most other Regex engines then you can use 's' (single-line) to allow `.` to match new-lines – doom87er Oct 24 '18 at 13:06
  • Sadly (depending on perspective) Dart uses JavaScript's regular expressions. Even in multi-line mode, `.` still doesn't match line terminators. – lrn Oct 24 '18 at 13:14
  • `[^]` is not a regex to match nothing. It's the beginning of a character class that won't match a right bracket, but it's a syntax error (incomplete character class)... you still need another right bracket, as in `[^]]`. A regex to match nothing would be something like `(?!)`. – Randal Schwartz Oct 24 '18 at 16:20
  • @RandalSchwartz the goal isn't to match nothing, it's to match any character including new-line. `[^...]` is a negated character class, it matches anything not present in the set. leaving the set empty allows it to match any character. I don't know what you mean by "match nothing" but a negative look ahead without a pattern would prevent your regex from matching anything – doom87er Oct 24 '18 at 16:34
  • OK, to match any character including newlines, I frequently use `[\d\D]`. – Randal Schwartz Oct 24 '18 at 16:37
1

Can you say why you need a RegExp? A function like:

String unquote(String input) {
  int start = input.indexOf('"');
  if (start < 0) return input; // or throw.
  int end = input.lastIndexOf('"');
  if (start == end) return input; // or throw
  return input.substring(start + 1, end);
}

is going to be faster and easier to understand than a RegExp.

Anyway, for the challenge, let's say we do want a RegExp that replaces the part up to the first " and from the last " with nothing. That's two replaces, so you can do an

input.replaceAll(RegExp(r'^[^"]*"|"[^"]*$'), "")`

or you can use a capturing group and a computed replacement like:

input.replaceFirstMapped(RegExp(r'^[^"]*"([^]*)"[^"]*$'), (m) => m[1])

Alternatively, you can use the capturing group to select the text between the two and extract it in code, instead of doing string replacement:

String unquote(String input) {
  var re = RegExp(r'^[^"]*"([^]*)"[^"]$');
  var match = re.firstMatch(input);  
  if (match == null) return input; // or throw.
  return match[1];
}
lrn
  • 64,680
  • 7
  • 105
  • 121