0

I'm having difficulty with matching a string literal with both newline(\n) and carriage return(\r) characters. For example:

"132 Holt Court, Cashtown, Rhode Island, 7680 \n\r"

What I tried is

\"([^\\\"]|\\.)*\"

But this regex can only match the string above without \r. Can anyone help me with this one?

Thank you.

Minh Thai
  • 519
  • 5
  • 18
  • 2
    You may want to keep in mind that the default is `\r\n` and not `\n\r`. See http://stackoverflow.com/questions/6539801/reminder-r-n-or-n-r – Dragondraikk Jun 01 '15 at 08:25
  • What are you trying to match exactly? The regex won't match anything since it is looking for `"`, then anything that is not `"` or a `.` (which does not make sense since it is already covered with `[^"]`), and then `"`. – Wiktor Stribiżew Jun 01 '15 at 08:25
  • Can you show your actual matching code and string? Where do you get the literal from, and are you sure what's inside it is actually a slash followed by an `r`, or maybe it happens to be an actual `\u000D` (carriage return)? – RealSkeptic Jun 01 '15 at 08:28
  • I'm trying to match a string literal, starts and end with `"`, contains escaped characters `\\.` or any character other than \ and `"` in the middle. – Minh Thai Jun 01 '15 at 08:30
  • @RealSkeptic yes the string is literally as above `\r\n` or `\n\r` doesn't matter – Minh Thai Jun 01 '15 at 08:33
  • Nevertheless, show the code, please. The one above doesn't have `"` in it at all, so please edit your question and add the actual code and show where you get the string, and how you try to match it. The answer could be a technical issue. And if you can convert the string to a char array, and print it with `Arrays.toString()` and add the result, properly formatted, to your question it would be helpful as well. Try to be as informative as you can if you want to get a good answer. – RealSkeptic Jun 01 '15 at 08:38
  • @RealSkeptic thanks, I edited the question to include `"`. I'm trying to write a parser for JSON, the code is just that regex. – Minh Thai Jun 01 '15 at 08:41
  • 2
    The code is **not** just that regex. That's not a valid Java program. I'm going to vote for closing the question, as [questions asking for debugging help on StackOverflow must include the code necessary to reproduce the problem](http://stackoverflow.com/help/on-topic). – RealSkeptic Jun 01 '15 at 08:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79295/discussion-between-minh-thai-and-realskeptic). – Minh Thai Jun 01 '15 at 08:48

1 Answers1

0

you can always remove them first before the check .

for example:

String s = "132 Holt Court, Cashtown, Rhode Island, 7680 \n\r"
s=s.replace("\\n\\r","\\n");
nafas
  • 5,004
  • 1
  • 23
  • 47