-1

I have a piece of code that splits a string into array of String.Becuase of quotes contains in a string it is unable to split using comma.

The regex is a part of my code so I can't remove the whole regex what I can do is I can expand it

 String  lines = "70001223,fjdhsd,jahd\",37874,dfhjkd,jadhj";

         String columns [] = lines.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);


         for(int i = 0; i < columns.length; i++){
             System.out.println(columns[i]);
         }

70001223,fjdhsd,**jahd"**
37874
dfhjkd
jadhj

this is the actual result but what I want the actual result is

70001223,
fjdhsd,
**jahd"**
37874
dfhjkd
jadhj

see the difference the quote(") is causing problem.

Harsh Kumrawat
  • 150
  • 1
  • 13
  • 2
    Why not just use : `String columns[] = lines.split(",");`? – Nicholas K Apr 20 '19 at 07:22
  • 1
    @NicholasK according to him there are "" quotes in his strings and hence he is not able to use split directly as indicated by you. – Onkar Musale Apr 20 '19 at 07:23
  • As I already mentioned the whole regex is a part of my code and I can't reduce the regex just I can change it to modify according to accept " – Harsh Kumrawat Apr 20 '19 at 07:23
  • 1
    What does splitting by comma have to do with quotes in the string? And why do the first two tokens contain a comma and the others don't? – derpirscher Apr 20 '19 at 07:25
  • refer this first- https://stackoverflow.com/a/18893443/8098322 – Onkar Musale Apr 20 '19 at 07:26
  • 1
    the sample data is not the best, but I also can't see what the double quotes matter; neither how you expect getting this `**jahd"**` which I can't find in the input data. And how you can change the regex but not reduce it??? – user85421 Apr 20 '19 at 07:29
  • I would understand that whole thing, if there were two quotes in the example. So maybe you must not split at a comma between two quotes (for instance csv file) but that's nothing you should do with a regex. Furthermore, where do the asterisks in your result come from? What is the rule for generating them, because they are not in your source string? – derpirscher Apr 20 '19 at 07:37
  • @derpirscher,@Cardlos, this comes from a file and need to split into the array.The actual source string is different which allows special characters – Harsh Kumrawat Apr 20 '19 at 07:43
  • @HarshKumrawat irrelevant, still does not explain what is the problem with quotes (based on that sample data you provided) Since you are probably using the regex from the question/answer linked by Onkar, you should also note what is written there: *"This will work provided you have **balanced quotes** in your string."* – user85421 Apr 20 '19 at 07:45
  • The problem with the quotes is doesn't allow the splitting.As the characters are generally splitted. – Harsh Kumrawat Apr 20 '19 at 07:46
  • 1
    why do quotes not allow splitting? it is just that expression that does not work with an odd number of quotes, or? with `var text="abc,\"',+-";` `text.split(",")` returns `String[3] { "abc", "\"'", "+-" }` - or using your example: `String[6] { "70001223", "fjdhsd", "jahd\"", "37874", "dfhjkd", "jadhj" }` - maybe you could edit the question to explain better – user85421 Apr 20 '19 at 07:52
  • What I have done is I replace the quote in the particular String like this lines = lines.replace("\"",""); – Harsh Kumrawat Apr 20 '19 at 07:56
  • if you *just* want to do: [splitting a comma-separated string but ignoring commas in quotes](https://stackoverflow.com/q/1757065/85421) you should write so...and provide corresponding example – user85421 Apr 20 '19 at 07:58
  • "The regex is a part of my code so I can't remove the whole regex what I can do is I can expand it" is confusing, if you are able to expand it, then it means you can modify it, so you should also be able to reduce it to simple `split(",")`. What *specifically* prevents you from doing that? – Pshemo Apr 20 '19 at 09:55
  • @Pshemo in my code we have some other special characters as well.And so I do not want it to modify that much – Harsh Kumrawat Apr 20 '19 at 10:02
  • "in my code we have some other special characters as well" that suggests you have structure which should be *parsed* (probably using state machine), so regex may not be proper tool for this job. To avoid situation described as [XY problem](https://meta.stackexchange.com/q/66377) it is best to provide full context to avoid wasting time on creating solutions which will be rejected by reasons not specified in question. – Pshemo Apr 20 '19 at 10:32

1 Answers1

1

Didn't totally understand the output, like some lines had a comma and *** did show in the original string.

Maybe something like this?

public class RegexQuestion {

public static void main(String[] args) {
    String  lines = "70001223,fjdhsd,***jahd***\",37874,dfhjkd,jadhj";
    String  newLine = lines.replace("\"", "");

    String[] columns = newLine.split(",");

    for (String column : columns) System.out.println(column);
}

}

user3132720
  • 76
  • 1
  • 6