Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringSplit regex question: missing part of my string?

 
Noam Ingalls
Ranch Hand
Posts: 60
Chrome Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on how to split strings with String.split(); I am using double quotes as delimiters for the strings; just want to remove the double quotes and print out the stuff in between them. It's going strange though; why is String.split eating the second part of my string? I'm not sure where I'm making a mistake with the regex.



Expected output:




Output currently displayed: the "world" is missing--?!

>
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I ran the code, I got the expected output, "hello world".
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that your input String does not contain any double quotes.

Also note that there's no need to put the double quote into a character class. would produce the same results.

Also, let's say you had a String that did contain double quotes:


What would you expect the output to be in that case?
 
Noam Ingalls
Ranch Hand
Posts: 60
Chrome Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

Also, let's say you had a String that did contain double quotes:


What would you expect the output to be in that case?


Ack, you're right, there's a problem right there. How should I get it to ignore double quotes inside other double quotes though? I've tried escaping the quotes, but of course like you'd expect the string got split on those quotes anyway.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noam Ingalls wrote:
Jeff Verdegan wrote:

Also, let's say you had a String that did contain double quotes:


What would you expect the output to be in that case?


Ack, you're right, there's a problem right there. How should I get it to ignore double quotes inside other double quotes though? I've tried escaping the quotes, but of course like you'd expect the string got split on those quotes anyway.


You can certainly escape the quotes, and split on only unescaped quotes. But that can be a problem if your escape character can itself be escaped. So if, for instance \" means you want a literal quote that is not to be used for splitting, but if you have \\ meaning a literal \, then \\" means you want a literal backslash and then a quote that is used for splitting, so you can't just say "split on a quote not preceded by backslash". Rather, it would have to be "not preceded by an even number of backslashes", which is impossible in regex. There can be ways around it, depending on your specific requirements and possible inputs, but even if it's possible, you may need to do it in multiple steps, not just a single split(). Regex may not be an appropriate tool for what you're trying to do.
 
Noam Ingalls
Ranch Hand
Posts: 60
Chrome Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So for the above kind of example I'd need multiple splits? Hm... What other ways apart from regex and String.split() would there be to split a string, apart from String Tokenizer though?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic