0

i have a text file like:

"GET /opacial/index.php?op=results&catalog=1&view=1&language=el&numhits=10&query=\xce\x95\xce\xbb\xce\xbb\xce\xac\xce\xb4\xce\xb1%20--%20\xce\x95\xce\xb8\xce\xbd\xce\xb9\xce\xba\xce\xad\xcf\x82%20\xcf\x83\xcf\x87\xce\xad\xcf\x83\xce\xb5\xce\xb9\xcf\x82%20
--%20\xce\x99\xcf\x83\xcf\x84\xce\xbf\xcf\x81\xce\xaf\xce\xb1
&search_field=11&page=1

And i want to cut all the characters after the word "query" and before "&search". (bolds above). I am trying to cut the data, using patterns but something is wrong.. Can you give me an example for the example code above?

EDIT: An other problem , except the one above is that the matcher is used only for charSequences, and i have a file, which can not casted to charSequence... :\

user758084
  • 35
  • 1
  • 8

2 Answers2

3

something like that:

   String yourNewText=yourOldText.split("query")[1].split("&search")[0];

?

to see how to read a file into a String, you can look here (there are different possiblities)

Community
  • 1
  • 1
Neifen
  • 2,546
  • 3
  • 19
  • 31
  • i am not sure, if this will do the job, cause my "yourOldText" is a file... How can this happen with a file? – user758084 Sep 19 '11 at 16:58
  • @user758084 read the file into a string via something like Apache Commons FileUtils.readFileToString. Notice that the regex solution I proposed would also require reading the file into a string. – John B Sep 19 '11 at 17:30
0
".*query\\=(.*)\\&search_field.*"

This regex should work to give you a capture of what you want to remove. Then String.replace should do the trick.

Edit - response to comment. The following code...

    String s = "GET /opacial/index.php?op=results&catalog=1&view=1&language=el&numhits=10&query=\\xce\\x95\\xce\\xbb\\xce\\xbb\\xce\\xac\\xce\\xb4\\xce\\xb1%20--%20\\xce\\x95\\xce\\xb8\\xce\\xbd\\xce\\xb9\\xce\\xba\\xce\\xad\\xcf\\x82%20\\xcf\\x83\\xcf\\x87\\xce\\xad\\xcf\\x83\\xce\\xb5\\xce\\xb9\\xcf\\x82%20 --%20\\xce\\x99\\xcf\\x83\\xcf\\x84\\xce\\xbf\\xcf\\x81\\xce\\xaf\\xce\\xb1&search_field=11&page=1";
    Pattern p = Pattern.compile(".*query\\=(.*)\\&search_field.*");
    Matcher m = p.matcher(s);
    if (m.matches()){
        String betweenQueryAndSearch = m.group(1);
        System.out.println(betweenQueryAndSearch);
    }

Produced the following output....

\xce\x95\xce\xbb\xce\xbb\xce\xac\xce\xb4\xce\xb1%20--%20\xce\x95\xce\xb8\xce\xbd\xce\xb9\xce\xba\xce\xad\xcf\x82%20\xcf\x83\xcf\x87\xce\xad\xcf\x83\xce\xb5\xce\xb9\xcf\x82%20 --%20\xce\x99\xcf\x83\xcf\x84\xce\xbf\xcf\x81\xce\xaf\xce\xb1
John B
  • 32,493
  • 6
  • 77
  • 98
  • the regex looks good but i am not sure of how i will use it with my file.. i have a file just like my first post, and i must keep only those between "query " and "&search"... how this will happen with the regex? – user758084 Sep 19 '11 at 17:04
  • it works exactly as you said, but when i change String s = .... with String s = File.toString(); i take no output... That means that there is nothing to match.. But the file i exactly the same with the paradigm above plus some other data... :\ – user758084 Sep 19 '11 at 17:33
  • Per the Javadoc. File.toString() returns the path of the File. http://download.oracle.com/javase/6/docs/api/java/io/File.html#toString() – John B Sep 19 '11 at 17:45
  • To read a file you must READ it using an InputStream. Try Apache commons FileUtils#readFileToString http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html#readFileToString(java.io.File) – John B Sep 19 '11 at 17:47