简体   繁体   中英

how to choose text from a file

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... :\\

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)

".*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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM