-1

I need to parse the email header (from, to) from gmail api and separate it example: "John Doe " become name: "John Doe" and email: "john.doe@gmail.com". I already found the solution, however it is in javascript. I need to convert it to java code. I have problem in change javascript regex to java:

const regex = /(([\w,\"\s]+)\s)?<?([^@<\s]+@[^@\s>]+)>?,/g;

This is my regex in java:

String regex = "(([\\w,\\"\\s]+)\\s)?<?([^@<\\s]+@[^@\\s>]+)>?";

I try to remove the first '/' and ',/g' character and also replace all '\' with '\\' character but still get error in regex.

This is not a duplicate of Print Hello Question because the problem is with matching the characters in the regex and not printing them. I already know about "\"" make " in java.

Sky Blue
  • 203
  • 5
  • 15
  • 1
    `\\"` will have a backslash in the string, then terminate the string. In order for regexp engine see `\"`, your Java String literal needs to be `\\\"`. That said, `\"` is useless, since `"` doesn't need to be escaped for regexp; `"` suffices. – Amadan Sep 12 '18 at 01:31

1 Answers1

0

You need to escape the " too, otherwise it closes the String. "(([\\w,\\\"\\s]+)\\s)?<?([^@<\\s]+@[^@\\s>]+)>?"

As Amadan said, " does not require escaping in regex, so it can be "(([\\w,\"\\s]+)\\s)?<?([^@<\\s]+@[^@\\s>]+)>?"

Ricky Mo
  • 6,285
  • 1
  • 14
  • 30