aspose file tools*
The moose likes Beginning Java and the fly likes StringSplit regex question: missing part of my string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringSplit regex question: missing part of my string?" Watch "StringSplit regex question: missing part of my string?" New topic
Author

StringSplit regex question: missing part of my string?

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

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--?!

>


"How do you define fool?"
"I don't attempt it. I wait for demonstrations. They inevitably surpass my imagination."
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
When I ran the code, I got the expected output, "hello world".
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 11, 2012
Posts: 60

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 11, 2012
Posts: 60

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringSplit regex question: missing part of my string?