2

I am facing a problem that I don't know correctly split this line. I only need RandomAdresas0 100 2018 1.

 String line = Files.readAllLines(Paths.get(failas2)).get(userInp);
            System.out.println(line);
            arr = line.split("[\\s\\-\\.\\'\\?\\,\\_\\@]+");;

Content in line:

[Pastatas{pastatoAdresas='RandomAdresas0',pastatoAukstuSkaicius=100,pastatoPastatymoData=2018, pastatoButuKiekis=1}]
Billy Brown
  • 2,272
  • 23
  • 25
HenrikasB
  • 321
  • 1
  • 9
  • This does not look like a job for a simple split. Knowing nothing about the format beyond that one example line, it's hard to say how one would go about parsing it. At the simplest, one would probably first extract the part within the braces (using `indexOf` and `substring()`), then split on `,` to get the pairs, then on `=` to get keys and values. – OhleC Nov 26 '18 at 09:40
  • Why have you escaped everything? would just "[\\s-.'?,_@]+" not be what you're looking for? When in doubt, use something like https://regexr.com/ to test out regex – AleksW Nov 26 '18 at 09:42

3 Answers3

1

You can try this code (basically extracting a string between two delimiters):

String ss = "[Pastatas{pastatoAdresas='RandomAdresas0',pastatoAukstuSkaicius=100,pastatoPastatymoData=2018, pastatoButuKiekis=1}]";
Pattern pattern = Pattern.compile("=(.*?)[,}]");
Matcher matcher = pattern.matcher(ss);
while (matcher.find()) {
    System.out.println(matcher.group(1).replace("'", ""));
}

This output:

RandomAdresas0
100
2018
prasad_
  • 12,755
  • 2
  • 24
  • 36
  • You missed the last one – Joakim Danielson Nov 26 '18 at 10:11
  • Yes, I didn't see it in the question (the `1` was after the line break- I missed it alright (it was not visible). The code is updated. – prasad_ Nov 26 '18 at 10:12
  • Thank you! Solved. – HenrikasB Nov 26 '18 at 10:17
  • Is it possible to add this printed information to ArrayList? I have declared my ArrayList: private ArrayList quick = new ArrayList(); So if I want to add the printed information in it should it be something like that: quick.add(new Pastatas(String,int ,int ,int )); – HenrikasB Nov 26 '18 at 10:36
  • I guess it can be added. – prasad_ Nov 26 '18 at 10:40
  • Do i think right: I need to equate matcher.group (1) .replace ("'', '"); to string and then string to stringarray and in that way I can fill my ArrayList like quick.add(new obj(strinarr[0], stringarr[1]....) – HenrikasB Nov 26 '18 at 11:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184262/discussion-between-henrikasb-and-prasad). – HenrikasB Nov 26 '18 at 11:06
0

Remove all the characters before '{' including '{' Remove all the characters after '}' including '}' You can do the both by using indexOf method and substring.

Now you will left with only the following:

pastatoAdresas='RandomAdresas0',pastatoAukstuSkaicius=100,pastatoPastatymoData=2018, pastatoButuKiekis=1

After this read this [thread][1] : Parse a string with key=value pair in a map?

VarunKrish
  • 179
  • 1
  • 9
0

Here is a solution using a regular expression and the Pattern & Matcher classes. The values you are after can be retrieved using the group() method and you get all values by looping as long as find() returns true.

String data = "[Pastatas{pastatoAdresas='RandomAdresas0',pastatoAukstuSkaicius=100,pastatoPastatymoData=2018, pastatoButuKiekis=1}]";
Pattern pattern = Pattern.compile("=([^, }]*)");
Matcher matcher = pattern.matcher(data);
while (matcher.find()) {
    System.out.printf("[%d:%d] %s", matcher.start(), matcher.end(), matcher.group(1));
}

The matched value is in group 1, group 0 matches the whole reg ex

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52