2

I'm matching a string of a key value pair between characters "" with "(.*?)" how can I ignore any extra " characters within the value part.

example string {"1"=>"email@example.com"}

stephen
  • 33
  • 4
  • Could you please clarify *any extra " characters within the value part*? `"(.*?)"` finds all occurrences of substrings between double quotes. – Wiktor Stribiżew May 03 '20 at 18:58
  • @WiktorStribiżew Example: {"1"=>"somest"ring" } For the value part I want to extract exactly as they entered it. result= somest"ring – stephen May 03 '20 at 19:07
  • It is wrong, it should be `"somes\"tring"`. Please have it fixed on the provider's side. All workarounds are not nice. You might try `String pat = "(?<=\\{|=>)\"(.*?)\"(?=\\}|=>)"` but it might still fail in edge cases. See https://regex101.com/r/rduF7v/1 – Wiktor Stribiżew May 03 '20 at 19:10
  • @WiktorStribiżew agreed it's broken - this will see me through. thank you. – stephen May 03 '20 at 19:19

1 Answers1

0

You may use

String pat = "(?<=\\{|=>)\"(.*?)\"(?=\\}|=>)";

See the regex demo

Details

  • (?<=\{|=>) - a positive lookbehind that matches a location immediately preceded with { or =>
  • " - a double quotation mark
  • (.*?) - Group 1: any zero or more chars other than line break chars, as few as possible
  • " - a double quotation mark
  • (?=\}|=>) - a positive lookahead that matches a location immediately followed with } or =>.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563