• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

StringSplit regex question: missing part of my string?

 
Ranch Hand
Posts: 60
Firefox Browser Chrome 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--?!

>
 
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".
 
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
Firefox Browser Chrome 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
Firefox Browser Chrome 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?
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!