0
String text="99.91.220.138 - - [05/Oct/2015:06:37:26 +0300] "GET /R/0A/ID_0000013091.ism/QualityLevels(1469889)/Fragments(video=14795200000) HTTP/1.1" 200 658377 "-" "Firefox/1.5" "-" rt=0.000 ut="-" cs=HIT";

I want to read all the characters of this string and put the string between two empty spaces into an array:

- array[0] will be "99.91.220.138"



- array[1] will be "-"



- array[2] will be "-"

and now if there is a "[ ]" or quotation marks(" ") between two empty spaces,I want to get the string until the end of the marks like:

- array[3] will be "[05/Oct/2015:06:37:26 +0300]"



- array[4] will be "GET
   /R/0A/ID_0000013091.ism/QualityLevels(1469889)/Fragments(video=14795200000)
   HTTP/1.1"

after or before this I need to remove the "-" and something not necessary from the array to have more performance.

.

What kind of suggestion do you have except using regular expressions?

Ali Göktaş
  • 15
  • 1
  • 7
  • Could you elaborate why you don't want a solution using a regular expression? This seems to be an actual good use-case for one. – Ben Jul 06 '18 at 09:42
  • My team leader does not allow to use regex – Ali Göktaş Jul 06 '18 at 09:44
  • Use String#slpit by space `text.split(" ")` – Amit Bera Jul 06 '18 at 09:45
  • What about the "[ " – Ali Göktaş Jul 06 '18 at 09:46
  • 1
    What a weird choice by your team leader. Well, that rules `String.split` out, too. Uses a regex. You should probably convert your String to a `char[]`, do it in some very complicated and obfuscated way and then maybe talk to your team leader that straight up not allowing regular expressions is maybe not the way to go. – Ben Jul 06 '18 at 09:47
  • To get you started with an idea: Loop over your `char[]`, set some boolean flag to true upon encountering an `[`, ignore whitespaces until you encounter a `]` where you set your flag to false again. That should be easy enough to implement. – Ben Jul 06 '18 at 09:51
  • Posting questions about homework is ok. You don't have to pretend your teacher is a team leader. :) All you need in your loop is some state information. when you encounter a bracket, you set the state to "in bracket" and just blast through everything until you find a closing bracket. Same kind of state with quotation marks. The next assignment will be nested brackets and then you will have to use a counter that keeps track of how deeply you are nested. – Torben Jul 06 '18 at 11:23

1 Answers1

0

I would recommend you to use the split function.

So for your first functionality, you use:

string.split(" ");

To keep the quoted statements this might help you:

Split a string by whitespace, keeping quoted segments, allowing escaped quotes

L_J
  • 2,351
  • 10
  • 23
  • 28
  • Your solution entirely relies on regular expressions (as does your linked solution for the second problem) which was ruled out by the OP. – Ben Jul 06 '18 at 09:50
  • string.split(" ") does not help me because I need to get also the string between '[' and ']' – Ali Göktaş Jul 06 '18 at 09:50
  • I did this with regex but now I need to do it with different way. – Ali Göktaş Jul 06 '18 at 09:52
  • Oh you are right! Maybe you could implement a iterator that checks every character in the array. Then every time you fine a quote, change a boolean that splits the upcomming array on meeting a space or squared bracket or not depending on that boolean? – Rwatnberzje Jul 06 '18 at 10:59